简体   繁体   中英

How can I get tweets from new twitter api 1.1?

I have just implemented https://github.com/thujohn/twitter-l4 for Laravel 4, it basically pulls in an array of tweets from a search of a hashtag.

It seems like its working, I think there are some things that are not working though.

I get a blank screen with no errors which means a positive sign.

Here is my code.

PHP:

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

This code basically gives me an array of 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

So from that I wrote this:

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

I get no errors on the page. Can anyone point me in the right direction please?

Cheers

Since each $tweet is an array so you should use $tweet['text'] instead of $tweet->text

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

Also $tweets->statuses should be $tweets['statuses'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM