note 95667 deleted from function.json-encode by crell

From: Date: Thu, 30 Jun 2022 20:01:57 +0000
Subject: note 95667 deleted from function.json-encode by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218798@lists.php.net to get a copy of this message
Note Submitter: garydavis at gmail dot com ---- 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 ?>

« previous php.notes (#218798) next »