当前位置: 首页>>代码示例>>PHP>>正文


PHP Tweet::loadFromArray方法代码示例

本文整理汇总了PHP中Tweet::loadFromArray方法的典型用法代码示例。如果您正苦于以下问题:PHP Tweet::loadFromArray方法的具体用法?PHP Tweet::loadFromArray怎么用?PHP Tweet::loadFromArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Tweet的用法示例。


在下文中一共展示了Tweet::loadFromArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: archive

 /**
  * Grabs all the latest tweets (up to 3200 because of API limits) and puts them into the database.
  *
  * @param string $type 'timeline'|'favorites'
  * @return string Returns a string with informational output.
  */
 public function archive($type = \Darathor\Amt\Archiver::TYPE_TIMELINE)
 {
     // This should use a maximum of 16 API calls if the user has 3200+ tweets.
     // api params
     $maxId = null;
     $sinceId = null;
     $userId = null;
     // not needed if using screen name
     $screenName = $this->username;
     $count = 200;
     $trimUser = null;
     $excludeReplies = false;
     $contributorDetails = true;
     $includeRts = true;
     $includeEntities = true;
     // loop variables
     $str = '';
     $page = 1;
     $gotResults = true;
     $apiCalls = 0;
     $tweetsFound = 0;
     $numAdded = 0;
     $numExceptions = 0;
     $maxExceptions = 5;
     // Don't get stuck in the loop if twitter is down.
     $maxPage = 2;
     // 20
     while ($gotResults && $page <= $maxPage) {
         $str .= 'max id: ' . ($maxId === null ? 'null' : $maxId) . PHP_EOL;
         try {
             if ($type === 'favorites') {
                 $tweetResults = $this->twitter->favoritesList($userId, $screenName, $count, $sinceId, $maxId, $includeEntities);
             } else {
                 $tweetResults = $this->twitter->statusesUserTimeline($userId, $screenName, $sinceId, $count, $maxId, $trimUser, $excludeReplies, $contributorDetails, $includeRts);
             }
             $apiCalls++;
             $numResults = count($tweetResults);
             $tweetsFound += $numResults;
             if ($numResults == 0) {
                 $str .= 'NO tweets on page ' . $page . ', exiting.' . PHP_EOL;
                 $gotResults = false;
             } else {
                 $newestTweet = $tweetResults[0];
                 $oldestTweet = end($tweetResults);
                 $str .= $numResults . ' tweets on page ' . $page . ' (oldest: ' . $oldestTweet['id'] . ', newest: ' . $newestTweet['id'] . ')' . PHP_EOL;
                 $page++;
                 // Add these tweets to the database.
                 $tweets = [];
                 foreach ($tweetResults as $t) {
                     $tweet = new Tweet();
                     $tweet->loadFromArray($t);
                     $tweets[] = $tweet;
                 }
                 $result = $this->model->addTweets($tweets);
                 if ($result === false) {
                     $str .= 'ERROR INSERTING TWEETS INTO DATABASE: ' . $this->model->getLastErrorMessage() . PHP_EOL;
                 } else {
                     if ($result == 0) {
                         $str .= 'Zero tweets added.' . PHP_EOL;
                     } else {
                         $str .= $result . ' tweets added.' . PHP_EOL;
                         $numAdded += $result;
                     }
                 }
                 // set max ID to the ID of the oldest tweet we've received, minus 1
                 // be mindful of 32 bit platforms
                 $maxId = $this->decrement64BitInteger($oldestTweet['id']);
             }
             // check if we've reached the rate limit
             $rate = $this->twitter->getLastRateLimitStatus();
             if (isset($rate['remaining']) && isset($rate['limit'])) {
                 $str .= $rate['remaining'] . '/' . $rate['limit'] . PHP_EOL;
                 if ($rate['remaining'] <= 0) {
                     $str .= 'API limit reached for this hour. Try again later.' . PHP_EOL;
                     $gotResults = false;
                 }
             } else {
                 $str .= 'Rate limit headers missing from response. Twitter may be having problems. Try again later.' . PHP_EOL;
                 $gotResults = false;
             }
         } catch (\Exception $e) {
             $str .= 'Exception: ' . $e->getMessage() . PHP_EOL;
             $numExceptions++;
             // break out to avoid infinite looping while twitter is down
             if ($numExceptions >= $maxExceptions) {
                 $str .= 'Too many connection errors. Twitter may be down. Try again later.' . PHP_EOL;
                 $gotResults = false;
             }
         }
     }
     $str .= $apiCalls . ' API calls, ' . $tweetsFound . ' tweets found, ' . $numAdded . ' tweets saved' . PHP_EOL;
     return $str;
 }
开发者ID:Darathor,项目名称:archive-my-tweets,代码行数:99,代码来源:Archiver.php

示例2: testLoadArray

 public function testLoadArray()
 {
     $tweetData = array('id' => 293780221621067776, 'user' => array('id' => 14061545), 'created_at' => '2013-01-22 13:00:37', 'text' => "Archive My Tweets has a new look, and can now import your official twitter archive. https://t.co/e8HDtbYa", 'source' => '<a href="http://twitterrific.com" rel="nofollow">Twitterrific for Mac</a>', 'truncated' => 0, 'favorited' => 0, 'in_reply_to_status_id' => 0, 'in_reply_to_user_id' => 0, 'in_reply_to_screen_name' => 0);
     $t = new Tweet();
     $t->loadFromArray($tweetData);
     foreach (array_keys($tweetData) as $key) {
         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);
 }
开发者ID:Darathor,项目名称:archive-my-tweets,代码行数:14,代码来源:tweetTest.php


注:本文中的Tweet::loadFromArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。