Posted By

Pjotor on 03/29/11


Tagged

JSONP


Versions (?)

JSONP service wrapper


 / Published in: PHP
 

URL: http://pjotor.com

Tiny litter wrapper for returning stuff as a JSONP service.

  1. $service_callback = ($_REQUEST["callback"]) ? $_REQUEST["callback"] : false;
  2.  
  3. function jsonp( $data = array(), $callback = false ) {
  4. $json = json_encode($data);
  5. header("Content-type: application/json");
  6. return ($callback) ? "$callback(" . $json . ");" : $json;
  7. }
  8.  
  9. //might be a nice thing to return the proper header e.g "header("HTTP/1.0 200 OK");"
  10. echo jsonp( array( 'test' => 'JSONP test' ), $service_callback );

Report this snippet  

You need to login to post a comment.