I am running a Debian box with PHP v5.2.17. I am trying to get around the cross-domain issue with an XML file and am using this got to fetch any xml and return json:
<?php
header('content-type: application/json; charset=utf-8');
if( strlen($_GET["feed"]) >= 13 ) {
$xml = file_get_contents(urldecode($_GET["feed"]));
if($xml) {
$data = @simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($data);
echo isset($_GET["callback"]) ? "{$_GET[’callback’]}($json)" : $json;
}
}
?>
The problem is, its not returning valid json to jquery.. The start character is "(" and the end is ")" where jquery wants "[" as the start and "]" as the end. I've taken the output and used several online validation tools to check it..
Is there a way I can change these characters prior to sending back or pass json_encode options?
header('content-type: application/json; charset=utf-8');part? I've never had a problem withjson_encode()and I've never set a header like that either, but perhaps I should be.(and)You don't actually mean those in"{$_GET[’callback’]}($json)"? If yes, just remove them there? Otherwise I would be very suprised to findjson_encodereturning a value with enclosing parenthesis.json_encode()does return valid JSON, even with SimpleXML objects; the output passes any JSON validator and you can sucessfullyjson_decode()it. The actual problem has already been highlighted by several answer.