推特json格式在php


twitter json formatting in php

我是PHP新手。我在twitter api中创建了一个帐户,并设法以JSON格式从twitter获取数据,并想从JSON中提取文本和created_at字段,但即使在3天后也没有运气,最后的手段是这个地方。

我现在的代码:

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=verizon");
$encode =json_encode($tweets);
$decode = json_decode($encode,true);
foreach($decode as $tweet)
{
  echo "{$tweet->text} {$tweet->created_at}'n";
}

var_dump结果:

  1 => 
    object(stdClass)[34]
      public 'metadata' => 
        object(stdClass)[35]
          ...
      public 'created_at' => string 'Wed Jun 10 15:54:11 +0000 2015' (length=30)
      public 'id' => float 6.0866345858697E+17
      public 'id_str' => string '608663458586968064' (length=18)
      public 'text' => string 'RT @FortuneMagazine: .@ElonMusk wants to build the Comcast of outer space http://t.co/wUUaNFtZnC http://t.co/3CS0Zmd6eA' (length=119)
      public 'source' => string '<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>' (length=82)
      public 'truncated' => boolean false
      public 'in_reply_to_status_id' => null
      public 'in_reply_to_status_id_str' => null
      public 'in_reply_to_user_id' => null
      public 'in_reply_to_user_id_str' => null
      public 'in_reply_to_screen_name' => null
      public 'user' => 
        object(stdClass)[36]
          ...
      public 'geo' => null
      public 'coordinates' => null
      public 'place' => null
      public 'contributors' => null
      public 'retweeted_status' => 
        object(stdClass)[39]
          ...
      public 'is_quote_status' => boolean false
      public 'retweet_count' => int 22
      public 'favorite_count' => int 0
      public 'entities' => 
        object(stdClass)[56]
          ...
      public 'favorited' => boolean false
      public 'retweeted' => boolean false
      public 'possibly_sensitive' => boolean false
      public 'lang' => string 'en' (length=2)

Try

echo $tweet->{1}->text

如果在1以上不起作用,则尝试

echo $tweet['1']['text']
不是

$tweet->text