skip to Main Content

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>";

to display my results worked great. However, I’m trying to adapt it to use the $foreach function used in the tutorial, but it only returns the headings. Here is the full code (minus the access tokens)

<?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 />";
    }
?>

Any help would be greatly appreciated. Thanks in advance.

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>";

to display my results worked great. However, I’m trying to adapt it to use the $foreach function used in the tutorial, but it only returns the headings. Here is the full code (minus the access tokens)

<?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 />";
    }
?>

Any help would be greatly appreciated. Thanks in advance.

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>";

to display my results worked great. However, I’m trying to adapt it to use the $foreach function used in the tutorial, but it only returns the headings. Here is the full code (minus the access tokens)

<?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 />";
    }
?>

Any help would be greatly appreciated. Thanks in advance.

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

Answers


  1. maybe try in your foreach loop this

    foreach($string as $items => $value)
    
    Login or Signup to reply.
  2. The data that you want appears to be one level further into the array than you are currently going. Specifically, the data is within a statuses sub-array. You simply need to run your foreach at that level instead of the top level.

    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 />";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search