views:

10

answers:

2

im having trouble extracting the trend name and search query from the json response

$init = 'http://api.twitter.com/1/trends/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$init);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
foreach ($obj->trends as $trend) {
    print $trend->query;
    print $trend->name;
}
+1  A: 

if you do print_r($obj) you'll see that the trends are in a subarray

Array
(
    [0] => stdClass Object
        (
            [as_of] => 2010-09-28T01:32:13Z
            [trends] => Array
                (
                    [0] => stdClass Object
                        (
                            [query] => BlackBerry+PlayBook
                            [promoted_content] => 
                            [url] => http://search.twitter.com/search?q=BlackBerry+PlayBook
                            [name] => BlackBerry PlayBook
                            [events] => 
                        )
.......

so you must use this:

...
foreach ($obj[0]->trends as $trend) {
    print $trend->query;
    print $trend->name;
}
Galen
thank you very much :)
bsym
no upvote or answer?
Galen
A: 

Try this

<?php
$init = 'http://api.twitter.com/1/trends/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$init);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);


foreach ($obj[0]['trends'] as $trend) {
    print $trend['query'];
    echo "<br>";
    print $trend['name'];
    echo "<hr>";
}

?>
JapanPro