【问题标题】:How can I get tweets from new twitter api 1.1?如何从新的 twitter api 1.1 获取推文?
【发布时间】:2014-07-07 21:07:33
【问题描述】:

我刚刚为 Laravel 4 实现了https://github.com/thujohn/twitter-l4,它基本上拉入了一个数组 来自搜索主题标签的推文。

它似乎可以正常工作,但我认为有些事情无法正常工作。

我得到一个没有错误的空白屏幕,这意味着一个积极的迹象。

这是我的代码。

PHP:

$tweets = Twitter::getSearch(array('q' => 'secretsocial', 'count' => 100, 'format' => 'array'));
var_dump($tweets);

这段代码基本上给了我一个json数组:

array (size=2)
  'statuses' => 
    array (size=20)
      0 => 
        array (size=24)
          'metadata' => 
            array (size=2)
              'result_type' => string 'recent' (length=6)
              'iso_language_code' => string 'en' (length=2)
          'created_at' => string 'Fri May 16 14:28:14 +0000 2014' (length=30)
          'id' => int 467310562603331586
          'id_str' => string '467310562603331586' (length=18)
          'text' => string 'On that #merch #grind. #spotify #swag just got shipped in. #secretsocial #free #leeds #leedsuni… http://t.co/67phqjeg5W' (length=121)
          'source' => string '<a href="http://instagram.com" rel="nofollow">Instagram</a>' (length=59)
          'truncated' => boolean false
          'in_reply_to_status_id' => null
          'in_reply_to_status_id_str' => null
          'in_reply_to_user_id' => null
          'in_reply_to_user_id_str' => null
          'in_reply_to_screen_name' => null
          'user' => 
            array (size=40)
              'id' => int 167993141
              'id_str' => string '167993141' (length=9)
              'name' => string 'Maral Erol' (length=10)
              'screen_name' => string 'liaaca' (length=6)
              'location' => string 'Leeds / San Diego' (length=17)
              'description' => string 'Music/Culture/Entertainment Enthusiast.' (length=39)
              'url' => string 'http://t.co/FL3uuA6QcN' (length=22)
              'entities' => 
                array (size=2)
                  'url' => 
                    array (size=1)
                      'urls' => 
                        array (size=1)
                          0 => 
                            array (size=4)
                              'url' => string 'http://t.co/FL3uuA6QcN' (length=22)
                              'expanded_url' => string 'http://linkd.in/R70tjB' (length=22)
                              'display_url' => string 'linkd.in/R70tjB' (length=15)
                              'indices' => 
                                array (size=2)
                                  0 => int 0
                                  1 => int 22
                  'description' => 
                    array (size=1)
                      'urls' => 
                        array (size=0)
                          empty

所以我写了这个:

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

我在页面上没有收到任何错误。谁能指出我正确的方向吗?

干杯

【问题讨论】:

  • 是否可以使用 twitter-I4 包使用主题标签获取推文?

标签: php json twitter laravel


【解决方案1】:

因为每个$tweet 都是一个数组,所以你应该使用$tweet['text'] 而不是$tweet-&gt;text

foreach($tweets->statuses as $tweet) {
    echo $tweet['text'];
}

$tweets-&gt;statuses 也应该是 $tweets['statuses']

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 2012-12-05
    • 2014-02-13
    • 1970-01-01
    • 2013-06-14
    • 2012-12-01
    • 2014-02-15
    • 2022-01-23
    相关资源
    最近更新 更多