So I’m playing around with yahoo’s yql. I got it to generate a url and the url looks like it’s returning decoded json to me. If I attempt to decode the output of the yql url i get no results (so assuming i’m right there)
$c =curl_init("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20state%3D'delaware'%20and%20city%20%3D%20'smyrna'%20and%20query%3D'pizza'&format=json");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 20); // query times out after 20 seconds
$data = curl_exec($c); // I asked for data format to be in json in the query it appears to be returned decoded
curl_close($c);
//print_r($data);
//$arr[] = $data; (returning results in decoded json)
//print_r($arr); (returning results)
foreach($data->query->results->result as $result)
{
echo 'blah blah blah';
}
However, no matter how I do things I can’t seem to access the output in my foreach line. What am I doing wrong?
Thanks in advance
You have to decode the json string first, otherwise it remains a plain-text string:
Note I’ve added error handling – your code had none, and assumed that the curl call succeeded. This is a bad way to go, as you can NOT depend on external resources to present or functional. Always check if an external resource request succeeded before proceeding.