2

I have the following code that searches for tweets with the hashtag "baseball".

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

The output that i am getting is of the form :

Simple Twitter API Test

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
            )

I would like to print only the 'text' as well as the 'created at' fields . I am unable to decode json into an associative array and print it using the following code:

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

Please suggest as to what changes should be made to print the associative array.

2 Answers 2

1

Change the 2nd parameter of json_decode :

json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);

by

json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(), true);
Sign up to request clarification or add additional context in comments.

Comments

0

To decode a json as an associative array you just need to pass true as second parameter

json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(), true);

From documentation

assoc

When TRUE, returned objects will be converted into associative arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.