dongqu1783 2014-03-12 00:14 采纳率: 100%
浏览 54
已采纳

Twitter API - 尝试获取非对象的属性

Hey guys I am trying to get some tweets by using the twitter API but I get this error when I run it:

Trying to get property of non-object in C:\xampp\htdocs\final_app\search_tweets.php on line 26 (commented on the code which line it is.

Here is my code, can anyone give any suggestion?

<?php include "library/twitteroauth.php"; ?>
<?php
 $consumer = "xx";
 $consumersecret = "xx";
 $accesstoken = "xx";
 $accesstokensecret = "";

 $twitter = new TwitterOAuth($consumer, $consumersecret, $accesstoken, $accesstokensecret);

?>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Team 11 - Final App</title>
</head>
<body>
    <form action ="" method="POST">
        <label> Search : <input type="text" name="keyword"/></label>
    </form>
    <?php
        if (isset($_POST['keyword'])){
            $tweets = $twitter->get('https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword'].'&since_id=24012619984051000&max_id=250126199840518145&result_type=recent&count=50');

            foreach($tweets as $tweet){
                foreach($tweet as $t){
                    echo $t->text;   //THIS IS LINE 26
                }
            }
        }
    ?>
</body>

  • 写回答

1条回答 默认 最新

  • dongzongxun8491 2014-03-12 00:43
    关注

    In this case your response from Twitter API hasn't got any feeds. Other problem is that your foreach loop is not compatible with Twitter API v1.1 because the response is:

    {
        "statuses": [
            // list of feeds
        ],
        "search_metadata": [
            // extra info about your query
        ]
    }
    

    So your loop should looks like this:

    $tweets = $twitter->get(/* your url */);
    if(isset($tweets->statuses) && is_array($tweets->statuses)) {
        if(count($tweets->statuses)) {
            foreach($tweets->statuses as $tweet) {
                echo $tweet->text;
            }
        }
        else {
            echo 'The result is empty';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画