Garbage after JSON - PHP Array problem YQL
i'm using YQL to send data back to an iPhone app i'm developing. I've got a JSON parser on the iphone and a PHP page on my webhost.
This is the PHP:
<?p开发者_StackOverflow中文版hp
header('Content-type: application/json');
$arr = array();
$result = $_GET["q"];
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = "select * from search.web where query ='%s'"; //YQL query to retrieve search results
$value = "lindsay+lohan";
$yql_query_url = $yql_base_url . "?q=" . urlencode(sprintf($yql_query, $value)) . "&format=json";
$session = curl_init($yql_query_url);
$json = curl_exec($session);
curl_close($session);
$temp = json_decode($json);
$arr[] = $temp;
echo json_encode($arr);
?>
When i use my iphone app and attempt to retrieve it, it says "Json parse failed: Garbage after JSON"
And if i run the PHP file in a browser, i see all the JSON data fine but after it there is "[1]", which is screwing it up i think?
Any ideas?
Important: unless you specify the CURLOPT_RETURNTRANSFER
option, cURL will output the response and return true
. This is what happens here: the response (which is some JSON) is output directly to the browser, followed by the echo json_encode(array(1))
you do on the last line.
Either don't try to process the response, or use CURLOPT_RETURNTRANSFER
.
精彩评论