garydavis at gmail dot com
14-Jan-2010 03:38
If you are planning on using this function to serve a json file, it's
important to note that the json generated by this function is not ready
to be consumed by javascript until you wrap it in parens and add ";" to
the end.
It took me a while to figure this out so I thought I'd save others the aggravation.
<?php
header('Content-Type: text/javascript; charset=utf8');
header('Access-Control-Allow-Origin: http://www.example.com/');
header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
$file='rss.xml';
$arr = simplexml_load_file($file);//this creates an object from the xml file
$json= '('.json_encode($arr).');'; //must wrap in parens and end with semicolon
print_r($_GET['callback'].$json); //callback is prepended for json-p
?>