Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitter趋势PHP_Php_Json_Api_Twitter - Fatal编程技术网

Twitter趋势PHP

Twitter趋势PHP,php,json,api,twitter,Php,Json,Api,Twitter,我试图使用,但我有一些麻烦 因此,我尝试使用: <?php $init = 'http://api.twitter.com/1/trends/current.json?exclude=hashtags'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$init); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $result = curl_exec($ch); curl_close($ch);

我试图使用,但我有一些麻烦

因此,我尝试使用:

  <?php
$init = 'http://api.twitter.com/1/trends/current.json?exclude=hashtags';
$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>";
}

?>

我得到了这个错误:

注意:第11行的\index.php中未定义的偏移量:0


警告:第11行为foreach()\index.php提供的参数无效。您读取的JSON错误。您需要执行以下操作:

foreach ($obj['trends']['2011-02-23 18:00:00'] as $trend) {
    print $trend['query'];
    echo "<br>";
    print $trend['name'];
    echo "<hr>";
}
foreach($obj['trends']['2011-02-23 18:00:00']作为$trend){
打印$trend['query'];
回声“
”; 打印$trend['name']; 回声“
”; }
如果我没有错的话

$obj = json_decode($result, true);
将生成数组而不是对象。其次,使用twitter api非常简单:

<?php
function get_trends($woeid){
    return json_decode(file_get_contents("http://api.twitter.com/1/trends/".$woeid.".json?exclude=hashtags", true), false); 
}
$data = get_trends(23424848); //23424848 is woeid for India...
$trends = $data[0]->trends;
echo "<ul>";
if(!empty($trends)){
    foreach($trends as $trend){
        echo '<li><a href="'.$trend->url.'" target="_blank">'.$trend->name.'</a></li>';
    }
}
echo "</ul>";
?>