-
-
Save avil13/635fbc17c5fb3f82ca82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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