Forum Moderators: coopster

Message Too Old, No Replies

Error when trying to convert JSON to XML

         

alexey9

4:49 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



Hello,

I'm trying to convert JSON data to XML data using PHP.

I wrote script that do it well, but it gives error when input JSON data has "wrong data".

The error is:

<br/>
<b>Warning</b>
: Invalid argument supplied for foreach() in
<b>/home/re/xmlDestinationsQuery.php</b>
on line
<b>91</b>
<br/>

You can see it in <snip>.

The raw JSON data for this XML data is
<snip>

I think the error occurs when PHP script meets this line from raw JSON data:

"The H&#244;tel de Ville (French for \\"City Hall\\"), Paris "

I wonder, how can I parse those \\" symbols in PHP? I tried str_replace, but it didn't work for me. I've tried to use htmlentities, but it gives more errors.

PHP code is:

$panoFile = "http://www.example.com/map/get_panoramas.php?order=popularity&set= public&from=0&to=".$panoCount."&minx=".$lnglow."&miny=".$latlow."&maxx= ".$lnghigh."&maxy=".$lathigh."&size=small";
$file = fopen($panoFile, "r");
$panoQuery = stream_get_contents($file);
fclose($file);

$panoArray = json_decode($panoQuery, true);
$panoArrayNext = $panoArray['photos'];

// the line 91 which gives error is here:
foreach($panoArrayNext as $pano){

echo '<pano ';
echo 'photo_id="'.$pano['photo_id'].'" ';
echo 'photo_title="'.$pano['photo_title'].'" ';
echo 'photo_url="'.$pano['photo_url'].'" ';
echo 'photo_file_url="'.$pano['photo_file_url'].'" ';
echo 'lng="'.$pano['longitude'].'" ';
echo 'lat="'.$pano['latitude'].'" ';
echo 'width="'.$pano['height'].'" ';
echo 'upload_date="'.$pano['upload_date'].'" ';
echo 'owner_id="'.$pano['owner_id'].'" ';
echo 'owner_name="'.$pano['owner_name'].'" ';
echo 'owner_url="'.$pano['owner_url'].'" ';

echo '/>';
}

[edited by: dreamcatcher at 6:30 pm (utc) on Aug. 22, 2007]
[edit reason] no urls as per T.O.S [webmasterworld.com].Thanks [/edit]

eelixduppy

1:53 am on Aug 23, 2007 (gmt 0)



Can you please tell me what the following outputs:

$panoArrayNext = $panoArray['photos'];
echo '<pre>';
print_r($panoArrayNext);
echo '</pre>';
exit;

alexey9

7:09 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Yes, sure.

Without "header("Content-type: text/xml");" it gives:

[q]Notice: Undefined variable: panoArrayNext in /home/re/xmlDestinationsQuery1.php on line 91[/q]

You can check modified query (with <pre>....</pre) <snip>

[edited by: eelixduppy at 11:40 am (utc) on Aug. 23, 2007]
[edit reason] no personal urls, please [/edit]

eelixduppy

1:01 pm on Aug 24, 2007 (gmt 0)



Did you add that code to your existing code? If not, it isn't going to work as expected. Try replacing it in the code in your first post and see what it outputs. The error you are getting doesn't make sense to me.