我正在尝试解码我的Twitter查询,以便从查询中提取id_str
值,但它似乎不允许我提取它,任何想法?
如果我打印它的编码版本,它可以正常工作。我只需要获得id_str
值。
当前代码:
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=%23stackoverflow");
$json = json_decode($tweets);
if (!empty($json)) {
echo $json->id_str;
} else {
echo "This isn't working.";
}
非常感谢任何帮助!
更新: Twitter编码响应(为了这篇文章,我将其限制为一条推文。)
{"statuses":[{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Sun Mar 09 16:06:58 +0000 2014","id":442693030894510080,"id_str":"442693030894510080","text":"Toggle HTML radio button by clicking its label http:\/\/t.co\/6o0A5tO5DR","source":"Tweet Button<\/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":22477625,"id_str":"22477625","name":"A. Farber","screen_name":"farber72","location":"Bochum","description":"Work: dev for mobile devices.\r\nSpeak: Rus, Ger, Eng.","url":"http:\/\/t.co\/8tujSm3UN3","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/8tujSm3UN3","expanded_url":"http:\/\/afarber.de","display_url":"afarber.de","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":7,"friends_count":18,"listed_count":0,"created_at":"Mon Mar 02 12:00:24 +0000 2009","favourites_count":1,"utc_offset":3600,"time_zone":"Berlin","geo_enabled":false,"verified":false,"statuses_count":56,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000399873249\/c84db99fc8604a2e6c7628d782720821_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000399873249\/c84db99fc8604a2e6c7628d782720821_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"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":[{"url":"http:\/\/t.co\/6o0A5tO5DR","expanded_url":"http:\/\/stackoverflow.com\/q\/4213172\/165071?stw=2","display_url":"stackoverflow.com\/q\/4213172\/1650\u2026","indices":[47,69]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}],"search_metadata":{"completed_in":0.014,"max_id":442693030894510080,"max_id_str":"442693030894510080","next_results":"?max_id=442693030894510079&q=stackoverflow&count=1&include_entities=1","query":"stackoverflow","refresh_url":"?since_id=442693030894510080&q=stackoverflow&include_entities=1","count":1,"since_id":0,"since_id_str":"0"}}
答案 0 :(得分:0)
你可以这样做
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=%23stackoverflow");
$data = json_decode($tweets,true);
//print_r($data);
echo $data["statuses"][0]["user"]["id_str"];
注意:现在返回数据只有$data["statuses"]
的一个元素,所以如果有多个数据,那么你可以循环检索数据。
答案 1 :(得分:0)
echo $json->statuses[0]->id_str;