Skip to content

Instantly share code, notes, and snippets.

@avil13
Forked from cowboy/jsonp.php
Last active August 29, 2015 14:14
Show Gist options
  • Save avil13/635fbc17c5fb3f82ca82 to your computer and use it in GitHub Desktop.
Save avil13/635fbc17c5fb3f82ca82 to your computer and use it in GitHub Desktop.
<?PHP
# JSONP "callback" param explanation, via basic PHP script.
#
# "Cowboy" Ben Alman
# http://benalman.com/
# Set $data to something that will be serialized into JSON. You'll undoubtedly
# have your own code for this.
$data = array("some_key" => "some_value");
# Encode $data into a JSON string.
$json = json_encode($data);
# If a "callback" GET parameter was passed, use its value, otherwise null. Note
# that "callback" is a defacto standard for the JSONP function name, but it may
# be called something else, or not implemented at all. For example, Flickr uses
# "jsonpcallback" for their API.
$jsonp_callback = isset($_GET['callback']) ? $_GET['callback'] : null;
# If a JSONP callback was specified, print the JSON data surrounded in that,
# otherwise just print out the JSON data.
#
# Specifying no callback param would print: {"some_key": "some_value"}
# But specifying ?callback=foo would print: foo({"some_key": "some_value"})
print $jsonp_callback ? "$jsonp_callback($json)" : $json;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment