-
-
Save gorobey/243b89619afe8048c51377c820c7d22a to your computer and use it in GitHub Desktop.
Simple public Web API in PHP using JSONP
This file contains 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_Example.php | |
* Author: Kevin Lanni | |
* Description: Demonstrates how to create a simple public Web API using JSONP responses | |
* This is useful for opening Web APIs for public use without the need for proxying or other SOP work-arounds. | |
* This example does not demonstrate API keys or any other method of authentication. | |
*/ | |
// Supply a header to set the proper expectation for the client browser | |
header('Content-Type: application/json'); | |
// A simple error handler, so we can output the error to the requesting script instead of failing silently | |
function errHandler($errno, $errstr) { | |
switch ($errno) { | |
// You can specify specific response data depending on error type, but for this example we'll only set a default | |
default: | |
$result = Array( | |
"success" => false, | |
"error" => $errstr, | |
"params" => $_GET | |
); | |
if (isset($_GET["callback"])) { | |
echo $_GET["callback"] .'('. json_encode($result) .');'; | |
} else { | |
echo json_encode($result); | |
} | |
exit; | |
break; | |
} | |
} | |
// Set our errHandler function above to be the default error handler for PHP | |
set_error_handler("errHandler"); | |
// | |
// Here you would put your PHP code that handles the API requests and builds responses. | |
// | |
// What I like to do is set a variable such as "$result" to an assoc array for my JSON data, like so: | |
$result = array( | |
"success" => true, | |
"message" => "You have successfully implemented a simple Web API using JSONP!" | |
); | |
// Then I would convert it to JSON using json_encode at the end of the script, when I'm ready to generate the output. | |
// For error handling, I use trigger_error, which is as simple as: | |
// if ($some_condition == "unmet") { | |
// trigger_error("Some condition was not met!", E_USER_ERROR); // Where E_USER_ERROR can be any valid PHP error constant (http://www.php.net/manual/en/errorfunc.constants.php) | |
// } | |
// The script would stop executing as soon as the first error is detected, because we exit the script in the error handler. This helps prevent lockups. | |
// Of course, you may want to handle warnings less strictly and do something else for your warnings. See error handler. | |
// Uncomment this if-statement to see an error thrown for "Undefined variable: some_condition". Uncomment just the trigger_error statement to see a custom error message. | |
// Once we're done executing all the necessary code to generate our data, let's convert it to JSON and print it out | |
if (isset($_GET["callback"])) { | |
echo $_GET["callback"] .'('. json_encode($result) .');'; | |
} else { | |
// Just in case we're not making cross domain requests, we can forego the JSONP and just expect regular JSON | |
echo json_encode($result); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment