我有一个Twitter搜索脚本与Twitter搜索API。
我的代码在这里:
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "XXX",
'oauth_access_token_secret' => "XXX",
'consumer_key' => "XXX",
'consumer_secret' => "XXX"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?f=tweets&vertical=default&q=wish&src=typd';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo "<pre>";
print_r($string);
echo "</pre>";
?>
它用json返回给我一个样本搜索结果。让我举个例子:
Array
(
[statuses] => Array
(
[0] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)
[created_at] => Sat Aug 08 11:54:02 +0000 2015
[id] => 629983908336005122
[id_str] => 629983908336005122
[text] => RT @pilosopo_tasho: "Sometimes I wish I could unmeet, delete, forget people."
[source] => Twitter for Android
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_status_id_str] =>
[in_reply_to_user_id] =>
[in_reply_to_user_id_str] =>
[in_reply_to_screen_name] =>
[user] => Array
(
[id] => 1734364392
[id_str] => 1734364392
[name] => EL EDWARD
[screen_name] => qwerty12307
[location] => Tanauan City,Batangas
[description] => | BornAgain | Drummer | Rakker | 7 is ♥ | PogiSInceBirthxD | Artist | God's Servant & Believer | PARAMORE LOVER | https://t.co/CD0EQkiOUM
[url] =>
[entities] => Array
(
[description] => Array
(
[urls] => Array
(
[0] => Array
(
[url] => https://t.co/CD0EQkiOUM
[expanded_url] => https://www.facebook.com/WkM.Bobi
[display_url] => facebook.com/WkM.Bobi
[indices] => Array
(
[0] => 114
[1] => 137
)
)
)
)
)
[protected] =>
[followers_count] => 671
[friends_count] => 274
[listed_count] => 2
[created_at] => Fri Sep 06 07:56:03 +0000 2013
[favourites_count] => 1050
[utc_offset] => 28800
[time_zone] => Beijing
[geo_enabled] =>
[verified] =>
[statuses_count] => 6590
[lang] => fil
[contributors_enabled] =>
[is_translator] =>
[is_translation_enabled] =>
[profile_background_color] => 89C9FA
[profile_background_image_url] => http://pbs.twimg.com/profile_background_images/518335320815714304/IP4m7tYf.jpeg
[profile_background_image_url_https] => https://pbs.twimg.com/profile_background_images/518335320815714304/IP4m7tYf.jpeg
[profile_background_tile] => 1
[profile_image_url] => http://pbs.twimg.com/profile_images/629497679664381952/rcARf_sr_normal.jpg
[profile_image_url_https] => https://pbs.twimg.com/profile_images/629497679664381952/rcARf_sr_normal.jpg
[profile_banner_url] => https://pbs.twimg.com/profile_banners/1734364392/1438919080
[profile_link_color] => 3B94D9
[profile_sidebar_border_color] => 000000
[profile_sidebar_fill_color] => DDEEF6
[profile_text_color] => 333333
[profile_use_background_image] => 1
[has_extended_profile] =>
[default_profile] =>
[default_profile_image] =>
[following] =>
[follow_request_sent] =>
[notifications] =>
)
[geo] =>
[coordinates] =>
[place] =>
[contributors] =>
[retweeted_status] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)
[created_at] => Sat Aug 08 11:45:24 +0000 2015
[id] => 629981732717113344
[id_str] => 629981732717113344
[text] => "Sometimes I wish I could unmeet, delete, forget people."
[source] => Hootsuite
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_status_id_str] =>
[in_reply_to_user_id] =>
[in_reply_to_user_id_str] =>
[in_reply_to_screen_name] =>
[user] => Array
(
[id] => 938887459
[id_str] => 938887459
[name] => Pilosopo Tasyo
[screen_name] => pilosopo_tasho
[location] => Pilipinas
[description] => Matandang Pilosopo | Member: United Influencers | Email: contact@bobongquote.com
[url] => http://t.co/l52iYnJ89G
[entities] => Array
(
[url] => Array
(
[urls] => Array
(
[0] => Array
(
[url] => http://t.co/l52iYnJ89G
[expanded_url] => http://www.bobongquote.com
[display_url] => bobongquote.com
[indices] => Array
(
[0] => 0
[1] => 22
)
)
)
)
[description] => Array
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 370638
[friends_count] => 101
[listed_count] => 186
[created_at] => Sat Nov 10 11:43:47 +0000 2012
[favourites_count] => 3
[utc_offset] => 28800
[time_zone] => Beijing
[geo_enabled] => 1
[verified] =>
[statuses_count] => 52812
[lang] => en
[contributors_enabled] =>
[is_translator] =>
[is_translation_enabled] =>
[profile_background_color] => 022330
[profile_background_image_url] => http://abs.twimg.com/images/themes/theme15/bg.png
[profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme15/bg.png
[profile_background_tile] =>
[profile_image_url] => http://pbs.twimg.com/profile_images/577984458206281728/eOxv_H1X_normal.jpeg
[profile_image_url_https] => https://pbs.twimg.com/profile_images/577984458206281728/eOxv_H1X_normal.jpeg
[profile_banner_url] => https://pbs.twimg.com/profile_banners/938887459/1426637219
[profile_link_color] => 0084B4
[profile_sidebar_border_color] => A8C7F7
[profile_sidebar_fill_color] => C0DFEC
[profile_text_color] => 333333
[profile_use_background_image] => 1
[has_extended_profile] =>
[default_profile] =>
[default_profile_image] =>
[following] =>
[follow_request_sent] =>
[notifications] =>
)
[geo] =>
[coordinates] =>
[place] =>
[contributors] =>
[is_quote_status] =>
[retweet_count] => 66
[favorite_count] => 59
[entities] => Array
(
[hashtags] => Array
(
)
[symbols] => Array
(
)
[user_mentions] => Array
(
)
[urls] => Array
(
)
)
[favorited] =>
[retweeted] =>
[lang] => en
)
[is_quote_status] =>
[retweet_count] => 66
[favorite_count] => 0
[entities] => Array
(
[hashtags] => Array
(
)
[symbols] => Array
(
)
[user_mentions] => Array
(
[0] => Array
(
[screen_name] => pilosopo_tasho
[name] => Pilosopo Tasyo
[id] => 938887459
[id_str] => 938887459
[indices] => Array
(
[0] => 3
[1] => 18
)
)
)
[urls] => Array
(
)
)
[favorited] =>
[retweeted] =>
[lang] => en
)
[1] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)
[created_at] => Sat Aug 08 11:54:02 +0000 2015
[id] => 629983907157446656
[id_str] => 629983907157446656
[text] => RT @disneywords: The most fantastic, magical things can happen, and it all starts with a wish. –Jiminy Cricket (Pinocchio)
[source] => Twitter for Android
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_status_id_str] =>
[in_reply_to_user_id] =>
[in_reply_to_user_id_str] =>
[in_reply_to_screen_name] =>
[user] => Array
(
[id] => 960524995
[id_str] => 960524995
[name] => Sofi
[screen_name] => sofinur_
[location] =>
[description] => dreamer
[url] =>
[entities] => Array
(
[description] => Array
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 205
[friends_count] => 256
[listed_count] => 1
[created_at] => Tue Nov 20 13:52:03 +0000 2012
[favourites_count] => 20
[utc_offset] => 25200
[time_zone] => Bangkok
[geo_enabled] => 1
[verified] =>
[statuses_count] => 3096
[lang] => id
[contributors_enabled] =>
[is_translator] =>
[is_translation_enabled] =>
[profile_background_color] => 352726
[profile_background_image_url] => http://abs.twimg.com/images/themes/theme5/bg.gif
[profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme5/bg.gif
[profile_background_tile] =>
[profile_image_url] => http://pbs.twimg.com/profile_images/629941184132087809/0NtWuWk8_normal.jpg
[profile_image_url_https] => https://pbs.twimg.com/profile_images/629941184132087809/0NtWuWk8_normal.jpg
[profile_banner_url] => https://pbs.twimg.com/profile_banners/960524995/1434944169
[profile_link_color] => 6B340A
[profile_sidebar_border_color] => FFFFFF
[profile_sidebar_fill_color] => EADEAA
[profile_text_color] => 333333
[profile_use_background_image] => 1
[has_extended_profile] =>
[default_profile] =>
[default_profile_image] =>
[following] =>
[follow_request_sent] =>
[notifications] =>
)
[geo] =>
[coordinates] =>
[place] =>
[contributors] =>
[retweeted_status] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)
[created_at] => Sat Aug 08 11:45:39 +0000 2015
[id] => 629981798701920256
[id_str] => 629981798701920256
[text] => The most fantastic, magical things can happen, and it all starts with a wish. –Jiminy Cricket (Pinocchio)
[source] => Hootsuite
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_status_id_str] =>
[in_reply_to_user_id] =>
[in_reply_to_user_id_str] =>
[in_reply_to_screen_name] =>
[user] => Array
(
[id] => 126400767
[id_str] => 126400767
[name] => Disney Words
[screen_name] => disneywords
[location] => disneywordstwitter@yahoo.com
[description] => We can learn a lot of things from Disney characters, and sometimes they just help us express what we feel.
[url] => http://t.co/d5O0KztA4I
[entities] => Array
(
[url] => Array
(
[urls] => Array
(
[0] => Array
(
[url] => http://t.co/d5O0KztA4I
[expanded_url] => http://disneywords.me
[display_url] => disneywords.me
[indices] => Array
(
[0] => 0
[1] => 22
)
)
)
)
[description] => Array
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 3525208
[friends_count] => 0
[listed_count] => 7930
[created_at] => Thu Mar 25 19:13:32 +0000 2010
[favourites_count] => 0
[utc_offset] => -25200
[time_zone] => Pacific Time (US & Canada)
[geo_enabled] => 1
[verified] =>
[statuses_count] => 82010
[lang] => en
[contributors_enabled] =>
[is_translator] =>
[is_translation_enabled] =>
[profile_background_color] => 030824
[profile_background_image_url] => http://pbs.twimg.com/profile_background_images/676942340/92934d4a0422a2e865f16a51bfc1fc65.jpeg
[profile_background_image_url_https] => https://pbs.twimg.com/profile_background_images/676942340/92934d4a0422a2e865f16a51bfc1fc65.jpeg
[profile_background_tile] =>
[profile_image_url] => http://pbs.twimg.com/profile_images/1175908443/disneywordspp_normal.jpg
[profile_image_url_https] => https://pbs.twimg.com/profile_images/1175908443/disneywordspp_normal.jpg
[profile_banner_url] => https://pbs.twimg.com/profile_banners/126400767/1401374801
[profile_link_color] => 0C2352
[profile_sidebar_border_color] => FFFFFF
[profile_sidebar_fill_color] => 64738F
[profile_text_color] => 020A1F
[profile_use_background_image] => 1
[has_extended_profile] =>
[default_profile] =>
[default_profile_image] =>
[following] =>
[follow_request_sent] =>
[notifications] =>
)
[geo] =>
[coordinates] =>
[place] =>
[contributors] =>
[is_quote_status] =>
[retweet_count] => 85
[favorite_count] => 62
[entities] => Array
(
[hashtags] => Array
(
)
[symbols] => Array
(
)
[user_mentions] => Array
(
)
[urls] => Array
(
)
)
[favorited] =>
[retweeted] =>
[lang] => en
)
[is_quote_status] =>
[retweet_count] => 85
[favorite_count] => 0
[entities] => Array
(
[hashtags] => Array
(
)
[symbols] => Array
(
)
[user_mentions] => Array
(
[0] => Array
(
[screen_name] => disneywords
[name] => Disney Words
[id] => 126400767
[id_str] => 126400767
[indices] => Array
(
[0] => 3
[1] => 15
)
)
)
[urls] => Array
(
)
)
[favorited] =>
[retweeted] =>
[lang] => en
)..........
我的搜索JSON结果的一部分在上面。
现在我想从这个结果中得到推文([text])。
如何使用此JSON中的php获取所有[text]值?
答案 0 :(得分:0)
首先,我在问题中改变了部分代码。因为在引用json api时错了。
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
此部分由此改变
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(), true);
然后,需要一个循环来获取json数组中的所有推文。对于解决方案:
foreach($string['statuses'] as $tweets) {
echo $tweets['text'] . '<br>';
}
它适用于我。