Skip to content

Latest commit

 

History

History
 
 

11-RESTful-API

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

http://en.wikipedia.org/wiki/Representational_state_transfer

http://www.slimframework.com/

http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script

http://php.net/manual/fr/function.json-encode.php

"Access-Control-Allow-Origin":

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
?>