PHP JSON Twitter趋势

PHP JSON Twitter趋势,php,json,twitter,Php,Json,Twitter,任何人都可以发现此PHP的问题,但屏幕上没有显示任何内容: <?php function get_data($url) { $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $data = curl_exec($ch);

任何人都可以发现此PHP的问题,但屏幕上没有显示任何内容:

<?php
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

$url='http://search.twitter.com/trends.json';
$obj = json_decode(get_data($url));
foreach ($obj as $item) {
$trend = $item->name;
$link = $item->url;
echo "<a href='.$link.'>".$trend."</a>";
}
?>

您没有正确地在集合上循环。使用:

foreach ($obj->trends as $item) {
您的
$obj
是一个对象(只是一个
stdClass
),具有
trends
属性,该属性是一个具有
name
url
属性的对象数组。这反映了JSON的结构,如下所示:

{
    "trends": [
        {
            "name": "#yepthatsme",
            "url": "http://search.twitter.com/search?q=%23yepthatsme"
        },
        {
            "name": "Miley Citrus",
            "url": "http://search.twitter.com/search?q=Miley+Citrus"
        },
        /* lots more */
        {
            "name": "Keith Olbermann",
            "url": "http://search.twitter.com/search?q=Keith+Olbermann"
        }
    ],
    "as_of": "Sat, 22 Jan 2011 13:37:25 +0000"
}
var_dump($obj)怎么说