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

时间:2014-03-12 00:14:48

标签: php twitter

嘿伙计我试图通过使用twitter API来获取一些推文,但是当我运行它时出现了这个错误:

  

尝试获取非对象的属性   第26行的C:\ xampp \ htdocs \ final_app \ search_tweets.php(注释代码是哪行。

这是我的代码,任何人都可以提出任何建议吗?

<?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 个答案:

答案 0 :(得分:2)

在这种情况下,来自Twitter API的回复没有任何Feed。其他问题是您的foreach循环与Twitter API v1.1不兼容,因为响应是:

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

所以你的循环应该是这样的:

$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';
    }
}