コード例 #1
0
 /**
  * Returns an array of Tweet objects that are populated from a Twitter JSON file.
  *
  * @param string $filename
  * @return array|false
  */
 public function getTweetsInJsonFile($filename)
 {
     $tweets = [];
     if (!file_exists($filename) || file_exists($filename) && !is_readable($filename)) {
         return false;
     }
     $jsonString = file_get_contents($filename);
     if ($jsonString === false) {
         return false;
     }
     // the twitter format includes extra JS code, but we just want the JSON array
     $pattern = '/\\[.*\\]/s';
     $matchError = preg_match($pattern, $jsonString, $matches);
     // $matchError can be zero or false if not found or there was a failure
     if (!$matchError) {
         return false;
     }
     $jsonArrayString = $matches[0];
     $jsonTweets = json_decode($jsonArrayString);
     foreach ($jsonTweets as $tweet) {
         $t = new Tweet();
         $t->loadFromJsonObject($tweet);
         $tweets[] = $t;
     }
     return $tweets;
 }
コード例 #2
0
 public function testLoadJsonObject()
 {
     $user = new \stdClass();
     $user->id = 14061545;
     $tweetData = new \stdClass();
     $tweetData->id = 293780221621067776;
     $tweetData->user = $user;
     $tweetData->created_at = '2013-01-22 13:00:37';
     $tweetData->text = "Archive My Tweets has a new look, and can now import your official twitter archive. https://t.co/e8HDtbYa";
     $tweetData->source = '<a href="http://twitterrific.com" rel="nofollow">Twitterrific for Mac</a>';
     $tweetData->favorited = 0;
     $tweetData->in_reply_to_status_id = 0;
     $tweetData->in_reply_to_user_id = 0;
     $tweetData->in_reply_to_screen_name = 0;
     $t = new Tweet();
     $t->loadFromJsonObject($tweetData);
     foreach ($tweetData as $key => $value) {
         if ($key == 'user' || $key == 'text') {
             continue;
         }
         $this->assertEquals($tweetData->{$key}, $t->{$key});
     }
     $this->assertEquals($tweetData->user->id, $t->user_id);
     $this->assertEquals($tweetData->text, $t->tweet);
 }