PHP代码不显示循环数组

时间:2018-04-11 13:55:02

标签: php twitter

我是PHP新手,我正在尝试修改教程“使用PHP在8个简单步骤中构建您的第一个Twitter应用程序”来使用Twitter搜索API而不是状态。上一次迭代使用

echo "<pre>";
print_r($string);
echo "</pre>";

显示我的效果非常好。但是,我正在尝试调整它以使用本教程中使用的$ foreach函数,但它只返回标题。这是完整的代码(减去访问令牌)

<?php

require_once('TwitterAPIExchange.php');

/** Add a title.  Specify Twitter API to be used. We are using search. Then specify fields and requestMethod, bring back 5 most popular**/
echo "<h2>Top 5 Tweets for Cloud Computing</h2>";
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=Cloud Computing&count=5&result_type=popular';
$requestMethod = 'GET';

/** Connect to the Twitter API**/
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}

/* echo "<pre>";
print_r($string);
echo "</pre>"; */

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 "Friends: ". $items['user']['friends_count']."<br /><hr />";
    }
?>

非常感谢任何帮助。提前谢谢。

I am new to PHP and I'm trying to amend code from the tutorial "Build Your First Twitter App Using PHP in 8 Easy Steps" to use the Twitter search API rather than the status.  Previous iteration using 

echo "<pre>";
print_r($string);
echo "</pre>";

显示我的效果非常好。但是,我正在尝试调整它以使用本教程中使用的$ foreach函数,但它只返回标题。这是完整的代码(减去访问令牌)

<?php

require_once('TwitterAPIExchange.php');

/** Add a title.  Specify Twitter API to be used. We are using search. Then specify fields and requestMethod, bring back 5 most popular**/
echo "<h2>Top 5 Tweets for Cloud Computing</h2>";
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=Cloud Computing&count=5&result_type=popular';
$requestMethod = 'GET';

/** Connect to the Twitter API**/
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}

/* echo "<pre>";
print_r($string);
echo "</pre>"; */

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 "Friends: ". $items['user']['friends_count']."<br /><hr />";
    }
?>

非常感谢任何帮助。提前谢谢。

I am new to PHP and I'm trying to amend code from the tutorial "Build Your First Twitter App Using PHP in 8 Easy Steps" to use the Twitter search API rather than the status.  Previous iteration using 

echo "<pre>";
print_r($string);
echo "</pre>";

显示我的效果非常好。但是,我正在尝试调整它以使用本教程中使用的$ foreach函数,但它只返回标题。这是完整的代码(减去访问令牌)

<?php

require_once('TwitterAPIExchange.php');

/** Add a title.  Specify Twitter API to be used. We are using search. Then specify fields and requestMethod, bring back 5 most popular**/
echo "<h2>Top 5 Tweets for Cloud Computing</h2>";
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=Cloud Computing&count=5&result_type=popular';
$requestMethod = 'GET';

/** Connect to the Twitter API**/
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}

/* echo "<pre>";
print_r($string);
echo "</pre>"; */

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 "Friends: ". $items['user']['friends_count']."<br /><hr />";
    }
?>

非常感谢任何帮助。提前谢谢。

Top 5 Tweets for Cloud Computing
Array
(
    [statuses] => Array
        (
            [0] => Array
                (
                    [created_at] => Tue Apr 10 19:50:32 +0000 2018
                    [id] => 983794376915726343
                    [id_str] => 983794376915726343
                    [text] => Forget "Old man yells at cloud." Now it's, "Old man yells at cloud computing."
                    [truncated] => 
                    [entities] => Array
                        (
                            [hashtags] => Array
                                (
                                )

                            [symbols] => Array
                                (
                                )

                            [user_mentions] => Array
                                (
                                )

                            [urls] => Array
                                (
                                )

                        )

                    [metadata] => Array
                        (
                            [result_type] => popular
                            [iso_language_code] => en
                        )

                    [source] => Twitter Web Client
                    [in_reply_to_status_id] => 983790713132503040
                    [in_reply_to_status_id_str] => 983790713132503040
                    [in_reply_to_user_id] => 155581583
                    [in_reply_to_user_id_str] => 155581583
                    [in_reply_to_screen_name] => robbysoave
                    [user] => Array
                        (
                            [id] => 155581583
                            [id_str] => 155581583
                            [name] => Robby Soave
                            [screen_name] => robbysoave
                            [location] => 
                            [description] => @reason editor. @thedailybeast columnist. 2016 @forbes 30 Under 30 list. @TFASorg Novak Fellow. Currently writing a book about activism in the age of Trump.
                            [url] => 
                            [entities] => Array
                                (
                                    [url] => Array
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [url] => 
                                                            [expanded_url] => http://reason.com
                                                            [display_url] => reason.com
                                                            [indices] => Array
                                                                (
                                                                    [0] => 0
                                                                    [1] => 23
                                                                )

                                                        )

                                                )

                                        )

                                    [description] => Array
                                        (
                                            [urls] => Array
                                                (
                                                )

                                        )

                                )

                            [protected] => 
                            [followers_count] => 16817
                            [friends_count] => 1174
                            [listed_count] => 463
                            [created_at] => Mon Jun 14 14:45:39 +0000 2010
                            [favourites_count] => 4190
                            [utc_offset] => -14400
                            [time_zone] => Eastern Time (US & Canada)
                            [geo_enabled] => 1
                            [verified] => 1
                            [statuses_count] => 20014
                            [lang] => en
                            [contributors_enabled] => 
                            [is_translator] => 
                            [is_translation_enabled] => 
                            [profile_background_color] => 022330
                            [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] => 1
                            [default_profile] => 
                            [default_profile_image] => 
                            [following] => 
                            [follow_request_sent] => 
                            [notifications] => 
                            [translator_type] => none
                        )

                    [geo] => 
                    [coordinates] => 
                    [place] => 
                    [contributors] => 
                    [is_quote_status] => 
                    [retweet_count] => 260
                    [favorite_count] => 1363
                    [favorited] => 
                    [retweeted] => 
                    [lang] => en
                )

            [1] => Array
                (
                    [created_at] => Tue Apr 10 11:00:01 +0000 2018
                    [id] => 983660869904228352
                    [id_str] => 983660869904228352
                    [text] => Google can now use cloud computing, geo-mapping and machine learning to prevent illegal fishing… 
                    [truncated] => 1
                    [entities] => Array
                        (
                            [hashtags] => Array
                                (
                                )

                            [symbols] => Array
                                (
                                )

                            [user_mentions] => Array
                                (
                                )

                            [urls] => Array
                                (
                                    [0] => Array
                                        (
                                            [url] => 
                                            [expanded_url] => https://twitter.com/i/web/status/983660869904228352
                                            [display_url] => twitter.com/i/web/status/9…
                                            [indices] => Array
                                                (
                                                    [0] => 97
                                                    [1] => 120
                                                )

                                        )

                                )

                        )

                    [metadata] => Array
                        (
                            [result_type] => popular
                            [iso_language_code] => en
                        )

                    [source] => Sprinklr
                    [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] => 91478624
                            [id_str] => 91478624
                            [name] => Forbes
                            [screen_name] => Forbes
                            [location] => New York, NY
                            [description] => Official Twitter account of ?? homepage for the world's business leaders.
                            [url] => 
                            [entities] => Array
                                (
                                    [url] => Array
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [url] => 
                                                            [expanded_url] => http://forbes.com
                                                            [display_url] => forbes.com
                                                            [indices] => Array
                                                                (
                                                                    [0] => 0
                                                                    [1] => 22
                                                                )

                                                        )

                                                )

                                        )

                                    [description] => Array
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [url] => 
                                                            [expanded_url] => http://Forbes.com
                                                            [display_url] => Forbes.com
                                                            [indices] => Array
                                                                (
                                                                    [0] => 28
                                                                    [1] => 48
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [protected] => 
                            [followers_count] => 14757421
                            [friends_count] => 5712
                            [listed_count] => 54795
                            [created_at] => Sat Nov 21 02:09:57 +0000 2009
                            [favourites_count] => 8639
                            [utc_offset] => -14400
                            [time_zone] => Eastern Time (US & Canada)
                            [geo_enabled] => 
                            [verified] => 1
                            [statuses_count] => 179200
                            [lang] => en
                            [contributors_enabled] => 
                            [is_translator] => 
                            [is_translation_enabled] => 1
                            [profile_background_color] => 072250
                            [profile_background_image_url] => http://pbs.twimg.com/profile_background_images/457626324/twitter_background_dark3.png
                            [profile_background_image_url_https] => https://pbs.twimg.com/profile_background_images/457626324/twitter_background_dark3.png
                            [profile_background_tile] => 
                            [profile_image_url] => http://pbs.twimg.com/profile_images/882603270484766720/YFx4Lsh4_normal.jpg
                            [profile_image_url_https] => https://pbs.twimg.com/profile_images/882603270484766720/YFx4Lsh4_normal.jpg
                            [profile_banner_url] => https://pbs.twimg.com/profile_banners/91478624/1514830219
                            [profile_link_color] => 072250
                            [profile_sidebar_border_color] => CCCCCC
                            [profile_sidebar_fill_color] => F7F7F7
                            [profile_text_color] => 000000
                            [profile_use_background_image] => 1
                            [has_extended_profile] => 
                            [default_profile] => 
                            [default_profile_image] => 
                            [following] => 
                            [follow_request_sent] => 
                            [notifications] => 
                            [translator_type] => regular
                        )

                    [geo] => 
                    [coordinates] => 
                    [place] => 
                    [contributors] => 
                    [is_quote_status] => 
                    [retweet_count] => 48
                    [favorite_count] => 100
                    [favorited] => 
                    [retweeted] => 
                    [possibly_sensitive] => 
                    [lang] => en
                )

2 个答案:

答案 0 :(得分:2)

您想要的数据似乎比您当前的数据更进一步。具体而言,数据在statuses子阵列内。您只需在 级别而不是顶级级别运行foreach

foreach($string['statuses'] 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 "Friends: ". $items['user']['friends_count']."<br /><hr />";
}

答案 1 :(得分:-2)

也许可以试试你的foreach循环

foreach($string as $items => $value)