tags:

views:

8214

answers:

4

Trends data from Twitter Search API in JSON.

Grabbing the file using:

$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

How do I work with data from this object. As an array? Only really need to extract data from the [name] values.

JSON object contains:

stdClass Object
(
    [trends] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Vote
                    [url] => http://search.twitter.com/search?q=Vote
                )

            [1] => stdClass Object
                (
                    [name] => Halloween
                    [url] => http://search.twitter.com/search?q=Halloween
                )

            [2] => stdClass Object
                (
                    [name] => Starbucks
                    [url] => http://search.twitter.com/search?q=Starbucks
                )

            [3] => stdClass Object
                (
                    [name] => #flylady
                    [url] => http://search.twitter.com/search?q=%23flylady
                )

            [4] => stdClass Object
                (
                    [name] => #votereport
                    [url] => http://search.twitter.com/search?q=%23votereport
                )

            [5] => stdClass Object
                (
                    [name] => Election Day
                    [url] => http://search.twitter.com/search?q=%22Election+Day%22
                )

            [6] => stdClass Object
                (
                    [name] => #PubCon
                    [url] => http://search.twitter.com/search?q=%23PubCon
                )

            [7] => stdClass Object
                (
                    [name] => #defrag08
                    [url] => http://search.twitter.com/search?q=%23defrag08
                )

            [8] => stdClass Object
                (
                    [name] => Melbourne Cup
                    [url] => http://search.twitter.com/search?q=%22Melbourne+Cup%22
                )

            [9] => stdClass Object
                (
                    [name] => Cheney
                    [url] => http://search.twitter.com/search?q=Cheney
                )

        )

    [as_of] => Mon, 03 Nov 2008 21:49:36 +0000
)
+13  A: 

You mean something like this?

<?php

$jsonurl = "http://search.twitter.com/trends.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

foreach ( $json_output->trends as $trend )
{
    echo "{$trend->name}\n";
}
Peter Bailey
This is great. Thanks.
easy rep point, huh? =)
Seiti
exactly what I needed, too! thanks!
yuval
+2  A: 

Just use it like it was an object you defined. i.e.

$trends = $json_output->trends;
Zak
+1  A: 

i'm just trying this one,.. it's create fresh 20 trends..

<?php

$trends_url = "http://search.twitter.com/trends/daily.json";

// initialise the session
$ch = curl_init();

// Set the URL
curl_setopt($ch, CURLOPT_URL, $trends_url);

// Return the output from the cURL session rather than displaying in the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Execute the session, returning the results to $curlout, and close.
$curlout = curl_exec($ch);
curl_close($ch);

$response = json_decode($curlout, true);

foreach(array_keys($response['trends']) as $hours){
print("<h3>".$hours."</h3>");
for($c=0; $c < 20; $c++){
print( ($c+1).". : ".$response['trends'][$hours][$c]['name']."<br />\n");
}
}
// If you want to see the json_decode output just uncomment out the next 2 lines
// $v = var_export($response);
// echo $v;
?>
Aming
I prefer to use json_decode with the `true` set for the second parameter, too - I find it's much more natural to work with arrays in PHP than objects, or worse, a mixture of arrays and objects.
Alex JL