I’m trying to pull the daily trends off the Twitter API using cURL and PHP but I get Undefined index errors. What am I doing incorrectly?
Here’s my php code
<?php
$trends_url = "http://api.twitter.com/1/trends/daily.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $trend){
$url = $trend['query'];
$name = $trend['name'];
?>
<div class="trend">
<a href="http://www.twitter.com/<?php echo $url;?>"><?php echo $name;?></a>
</div
<?php
}
?>
shows the error:
Notice: Undefined index: query in C:\xampp\htdocs\twitter_trends\index.php on line 22
Notice: Undefined index: name in C:\xampp\htdocs\twitter_trends\index.php on line 24
You’re two levels off from the arrays with the names/queries.
print_r($response)if you’re confused.