【问题标题】:Echo JSON Twitter API (1.1) PHPEcho JSON Twitter API (1.1) PHP
【发布时间】:2014-08-06 00:50:45
【问题描述】:

我正在尝试显示来自 Twitter Search API 1.1 (GET) 的坐标。

我可以在对推文进行编码后输出整个 JSON 文件 - http://pastebin.com/bKLye2an

$search = str_replace("#", "%23", $search); 
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$search."&count=".$notweets);

$json = json_encode($tweets);

但是,在 PHP 中,我使用以下代码来显示坐标,但什么也没有出现。

 foreach ($json['statuses'] as $key => $value)
{
foreach ($json['coordinates']['coordinates'] as $key => $value)
{
echo "$key: $value\n";
};
};

据我所知,结构是地理的状态-坐标-坐标。

我如何为搜索中的所有推文回显坐标(如果存在)。

【问题讨论】:

  • 您不需要第二个 foreach 循环。试试$value['coordinates'];
  • 我删除了第二个循环 - pastebin.com/8i4tUucT - 仍然是同样的问题 - 空白输出。
  • 第一个循环将只属于$json['statuses'],而不属于$json['coordinates']['coordinates']
  • 正如 rikesh 所说,$value['coordinates'] 没有更多的子数组,它已经单独存在了。顺便说一句,您在坐标上的示例 json 是 null
  • 这是我的完整代码(减去密钥和令牌):pastebin.com/7LUAZuqx 仍然没有输出

标签: php json twitter


【解决方案1】:

鉴于您的示例 json 字符串,一个简单的 foreach 就足够了。考虑这个例子:

$json_string = '{"statuses":[{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Mon Jun 16 08:35:18 +0000 2014","id":478455763761786880,"id_str":"478455763761786880","text":"Later go shop at cold storage. LOLOL","source":"Twitter for Android<\/a>","truncated":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":{"id":559622523,"id_str":"559622523","name":"AhYong","screen_name":"likeasomebodeh","location":"To each and every road.","description":"Fun-Sized \/ CE1404N \/ BARVENGERS \/ Sagittarian \/ Sixth Gun \/ Drummer - Wannabe","url":"http:\/\/t.co\/PFSXy17655","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/PFSXy17655","expanded_url":"http:\/\/ask.fm\/Ah_Yong","display_url":"ask.fm\/Ah_Yong","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":329,"friends_count":444,"listed_count":1,"created_at":"Sat Apr 21 13:30:49 +0000 2012","favourites_count":4930,"utc_offset":28800,"time_zone":"Beijing","geo_enabled":false,"verified":false,"statuses_count":33116,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/459318345003593728\/mhjxKipm_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/459318345003593728\/mhjxKipm_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/559622523\/1401358655","profile_link_color":"009999","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"en"}],"search_metadata":{"completed_in":0.016,"max_id":478455763761786880,"max_id_str":"478455763761786880","next_results":"?max_id=478455763761786879&q=flu%20OR%20cold%20OR%20%23flu%20OR%20%23cold%20OR%20%23snot%20OR%20%23ill&count=1&include_entities=1","query":"flu+OR+cold+OR+%23flu+OR+%23cold+OR+%23snot+OR+%23ill","refresh_url":"?since_id=478455763761786880&q=flu%20OR%20cold%20OR%20%23flu%20OR%20%23cold%20OR%20%23snot%20OR%20%23ill&include_entities=1","count":1,"since_id":0,"since_id_str":"0"}}';
$json = json_decode($json_string, true);
foreach($json['statuses'] as $key => $json_values) {
    if(isset($json_values['coordinates']) && !empty($json_values['coordinates'])) {
        // continue your processes if key exists and not empty
    }

}

重要提示:在您给定的 json 中,请注意,您的 coordinatesnull

【讨论】:

  • 我试过上面的pastebin.com/W5ghUiMS我的输出现在是“ArrayArrayArray”
  • @CodyRaspien 请var_export($json2)。似乎您的 json 字符串示例与您在代码中使用的示例不匹配。
猜你喜欢
  • 2013-03-04
  • 2013-12-07
  • 2012-12-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
相关资源
最近更新 更多