我有以下代码,用标签“棒球”搜索推文。
<?php
echo "<h2>Simple Twitter API Test</h2>";
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "*****",
'oauth_access_token_secret' => "*****",
'consumer_key' => "****",
'consumer_secret' => "*****"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#baseball';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo "<pre>";
print_r($string);
echo "</pre>";
?>
我得到的输出格式为:
简单的Twitter API测试
Array
(
[statuses] => Array
(
[0] => Array
(
[metadata] => Array
(
[result_type] => recent
[iso_language_code] => en
)
[created_at] => Sun Feb 23 22:06:36 +0000 2014
[id] => 4.37710107788E+17
[id_str] => 437710107787657216
[text] => Good day at practice #springtraining #baseball http://t.co/Zu8zJ6qAeI
[source] => Instagram
[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] => 75486410
[id_str] => 75486410
[name] => Jimi Mosher
[screen_name] => zortyonroids
[location] =>
[description] =>
[url] =>
[entities] => Array
(
[description] => Array
(
[urls] => Array
(
)
)
)
[protected] =>
[followers_count] => 12
[friends_count] => 161
[listed_count] => 0
[created_at] => Sat Sep 19 05:58:13 +0000 2009
[favourites_count] => 6
[utc_offset] =>
[time_zone] =>
[geo_enabled] => 1
[verified] =>
[statuses_count] => 50
[lang] => en
[contributors_enabled] =>
[is_translator] =>
[is_translation_enabled] =>
[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] =>
[profile_image_url] => http://pbs.twimg.com/profile_images/378800000476940795/6c5ab18350233a756b9980dd2a2e84c1_normal.jpeg
[profile_image_url_https] => https://pbs.twimg.com/profile_images/378800000476940795/6c5ab18350233a756b9980dd2a2e84c1_normal.jpeg
[profile_banner_url] => https://pbs.twimg.com/profile_banners/75486410/1379568136
[profile_link_color] => 0084B4
[profile_sidebar_border_color] => C0DEED
[profile_sidebar_fill_color] => DDEEF6
[profile_text_color] => 333333
[profile_use_background_image] => 1
[default_profile] => 1
[default_profile_image] =>
[following] =>
[follow_request_sent] =>
[notifications] =>
)
[geo] =>
[coordinates] =>
[place] =>
[contributors] =>
[retweet_count] => 0
[favorite_count] => 0
[entities] => Array
(
[hashtags] => Array
(
[0] => Array
(
[text] => springtraining
[indices] => Array
(
[0] => 21
[1] => 36
)
)
[1] => Array
(
[text] => baseball
[indices] => Array
(
[0] => 37
[1] => 46
)
)
)
[symbols] => Array
(
)
[urls] => Array
(
[0] => Array
(
[url] => http://t.co/Zu8zJ6qAeI
[expanded_url] => http://instagram.com/p/kxlYF0yvfD/
[display_url] => instagram.com/p/kxlYF0yvfD/
[indices] => Array
(
[0] => 47
[1] => 69
)
)
)
[user_mentions] => Array
(
)
)
[favorited] =>
[retweeted] =>
[possibly_sensitive] =>
[lang] => en
)
我想只打印'text'以及'created at'字段 。 我无法将json解码为关联数组并使用以下代码打印它:
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
foreach($string as $items)
{
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet: ". $items['text']."<br />";
echo "Tweeted by: ". $items['user']['name']."<br />";
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br />";
echo "Friends: ". $items['user']['friends_count']."<br />";
echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
}
请建议应该对打印关联数组进行哪些更改。
答案 0 :(得分:1)
更改json_decode
的第二个参数:
json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
通过
json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(), true);
答案 1 :(得分:0)
要将json
解码为关联数组,您只需将true
作为第二个参数传递
json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(), true);
ASSOC
当为TRUE时,返回的对象将被转换为关联数组。