2

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?

4
  • does it work if you remove the header('content-type: application/json; charset=utf-8'); part? I've never had a problem with json_encode() and I've never set a header like that either, but perhaps I should be. Commented Jun 29, 2011 at 10:47
  • With ( and ) You don't actually mean those in "{$_GET[’callback’]}($json)"? If yes, just remove them there? Otherwise I would be very suprised to find json_encode returning a value with enclosing parenthesis. Commented Jun 29, 2011 at 10:49
  • your question title is not true. json_encode() does return valid JSON, even with SimpleXML objects; the output passes any JSON validator and you can sucessfully json_decode() it. The actual problem has already been highlighted by several answer. Commented Jun 29, 2011 at 10:58
  • Can you show us your jQuery ajax call? Commented Jun 29, 2011 at 11:01

4 Answers 4

1

You could change json_encode($data) to json_encode(array($data)) if it expects an array (like you're saying):

$json = json_encode(array($data));

EDIT: Also, I believe the SimpleXml call will result in a bunch of SimpleXmlElements, perhaps json_encode then thinks it should be objects, instead of arrays? Perhaps casting to an array will yield the correct results.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot json_encode() SimpleXMLElements (that's the type that is returned by simplexml_load_string(). You have to convert the data from the XML file into some native PHP type (most likely an array).

SORRY that's wrong. json_encode() can in fact encode SimpleXMLElements (at least on my PHP version 5.3.4). So if your client-side code expects an array you must wrap your $data in an array:

$json = json_encode(array($data));

1 Comment

I don't understand why an array created by iterator_to_array() also needs to be converted to array in order to be a valid array for json_encode
0

We can use json_encode() function most probably on array. so you first take XML content into PHP array and then apply json_encode().I think this will solve your problem..

Comments

0

It seems that you are sending an empty callback parameter or something, but the callback parameter in jQuery must look exactly like this: callback=?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.