dtq7387 2016-01-21 17:13
浏览 75
已采纳

注意:尝试获取非对象Twitter API的属性

I am using twitter API to get the tweets on website, sometimes it shows good and than suddenly its stop showing error..

This error comes when its stops working:

Notice: Trying to get property of non-object in public_html/wp-content/themes/sandra/TwitterModel.php on line 26

Here is my code:

require("TwitterModel.php");
$tw = new TwitterModel();
$t = $tw->getTweets("sandrasflower");
echo "<div>";
foreach($t['tweets'] as $date=>$tweet) 
{
   echo "<div class='tweet'>";
   echo $tweet . "<br />" . date("jS F Y", strtotime($date));
   echo "</div>";
}

Please help me somebody...

TwitterModel.php file code:

<?php

    class TwitterModel {

        protected $token        = "1949108046-8jQwOMdqLSw86n2UZvFx0RlkquyKDEwndxdTMfN";
        protected $token_secret     = "PE1EIHRGpt8OHkk5Okc9HLUE2ilUL1UYP4u3GTDucOPCG";
        protected $consumer_key     = "9b7Ww9SM1AdeiVhOoYuenDsRS";
        protected $consumer_secret  = "nhAASYEa4epSribnb1kSPqtZVFMih4eXYJVWgm5W5VBTCnuOjC";

        protected $host             = 'api.twitter.com';
        protected $method           = 'GET';
        protected $path             = '/1.1/statuses/user_timeline.json'; // api call path

        # Get the last 5 tweets for this screen name
        public function getTweets($user) {
            $query = array( // query parameters
                'screen_name' => $user,
                'count' => '4'
            );

            $twitter_data = $this->request($query);

            $name = "";
            $tweets = array();
            foreach ($twitter_data as $value) {
                $name = $value->user->name;
                $tweetout = "";
                $tweetout .= preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $value->text) . "<br />";
                $tweetout = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $tweetout);
                $tweetout = preg_replace("/#(\w+)/", "<a href=\"http://twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweetout);
                $tweets[$value->created_at] = $tweetout;
            }

            if(is_array($tweets) && !empty($tweets)) {
                return array("name" => $value->user->name, "image" => $value->user->profile_image_url, "tweets" => $tweets);
            } else {
                return false;
            }
        }

        public function request($query) {
            $oauth = array(
                'oauth_consumer_key' => $this->consumer_key,
                'oauth_token' => $this->token,
                'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
                'oauth_timestamp' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_version' => '1.0'
            );

            $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
            $query = array_map("rawurlencode", $query);

            $arr = array_merge($oauth, $query); // combine the values THEN sort

            asort($arr); // secondary sort (value)
            ksort($arr); // primary sort (key)

            // http_build_query automatically encodes, but our parameters
            // are already encoded, and must be by this point, so we undo
            // the encoding step
            $querystring = urldecode(http_build_query($arr, '', '&'));

            $this->url = "https://$this->host$this->path";

            // mash everything together for the text to hash
            $base_string = $this->method."&".rawurlencode($this->url)."&".rawurlencode($querystring);

            // same with the key
            $key = rawurlencode($this->consumer_secret)."&".rawurlencode($this->token_secret);

            // generate the hash
            $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

            // this time we're using a normal GET query, and we're only encoding the query params
            // (without the oauth params)
            $this->url .= "?".http_build_query($query);
            $this->url = str_replace("&amp;","&",$this->url); //Patch by @Frewuill

            $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
            ksort($oauth); // probably not necessary, but twitter's demo does it

            // also not necessary, but twitter's demo does this too
            function add_quotes($str) { return '"'.$str.'"'; }
            $oauth = array_map("add_quotes", $oauth);

            // this is the full value of the Authorization line
            $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

            // if you're doing post, you need to skip the GET building above
            // and instead supply query parameters to CURLOPT_POSTFIELDS
            $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                              //CURLOPT_POSTFIELDS => $postfields,
                              CURLOPT_HEADER => false,
                              CURLOPT_URL => $this->url,
                              CURLOPT_RETURNTRANSFER => true,
                              CURLOPT_SSL_VERIFYPEER => false);

            // do our business
            $feed = curl_init();
            curl_setopt_array($feed, $options);
            $json = curl_exec($feed);
            curl_close($feed);

            $twitter_data = json_decode($json);
            return $twitter_data;
        }

    }

?>
  • 写回答

1条回答 默认 最新

  • douyongwan5946 2016-01-21 17:57
    关注

    This is the error being sent back from your calls to Twitter:

    class stdClass#2 (1) {
      public $errors =>
      array(1) {
        [0] =>
        class stdClass#3 (2) {
          public $message =>
          string(19) "Rate limit exceeded"
          public $code =>
          int(88)
        }
      }
    }
    

    So you are making API calls more frequently than is allowed. See https://blog.twitter.com/2008/what-does-rate-limit-exceeded-mean-updated

    It's not a code problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题