tags:

views:

35

answers:

1

Hi, 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:

<?php
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?

A: 

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.

Victor Nicollet