2014-12-13 50 views
-1

我正在玩Twitter API,但我無法解析給出的結果。Twitter中的PHP解析JSON流

這是我的PHP代碼:

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


$new = json_encode($tweets); 
echo $new; 


foreach($new['statuses'] as $item) { 
    echo $item['text']; 
} 

答案是:

{ 
statuses: [ 
{ 
metadata: { 
iso_language_code: "ar", 
result_type: "recent" 
}, 
created_at: "Fri Dec 12 17:34:40 +0000 2014", 
id: 543458929817030000, 
id_str: "543458929817030657", 
text: "RT @FCOArabic: RT @ukhelps: فيديو: اختراع بريطاني يساعد أطفال #سورية اللاجئين لتعلم مهارات الكمبيوتر @raspberrypi #UKHELPS http://t.co/qpCc…", 
source: "Twitter for iPhone</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: 2903018041, 
id_str: "2903018041", 
name: "hiba ahmad", 
screen_name: "hibaahm98349727", 
location: "", 
profile_location: null, 
description: "", 
url: null, 
entities: { 
description: { 
urls: [ ] 
} 
}, 
protected: false, 
followers_count: 4, 
friends_count: 19, 
listed_count: 0, 
created_at: "Mon Nov 17 18:46:27 +0000 2014", 
favourites_count: 3, 
utc_offset: null, 
time_zone: null, 
geo_enabled: false, 
verified: false, 
statuses_count: 36, 
lang: "ar", 
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/543090320179601408/e5DYdo_Q_normal.jpeg", 
profile_image_url_https: "https://pbs.twimg.com/profile_images/543090320179601408/e5DYdo_Q_normal.jpeg", 
profile_banner_url: "https://pbs.twimg.com/profile_banners/2903018041/1418318037", 
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, 
retweeted_status: { 
metadata: { 
iso_language_code: "ar", 
result_type: "recent" 
}, 
created_at: "Thu Dec 11 22:05:16 +0000 2014", 
id: 543164639811740000, 
id_str: "543164639811735552", 
text: "RT @ukhelps: فيديو: اختراع بريطاني يساعد أطفال #سورية اللاجئين لتعلم مهارات الكمبيوتر @raspberrypi #UKHELPS http://t.co/qpCcHYXpup", 
source: "Hootsuite</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, 
} 

它要求包括hashtag #raspberrypi,我得到這個答案,我json_encode();

編碼我如何正確解析這個?我只想得到它的文本。

回答

0

我不確定你的$connection->get()方法返回什麼,但是如果它的JSON字符串應該使用json_decode而不是json_encode。

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

$tweetsArray = json_decode($tweets, true); 
foreach($tweetsArray['statuses'] as $item) { 
    echo $item['text']; 
}