Created
April 5, 2017 17:09
-
-
Save Pamblam/6152561979d51beb3bdad8498386bf09 to your computer and use it in GitHub Desktop.
Simple starting point for PHP webservices
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 | |
session_start(); | |
// Create the return array | |
$return = array( | |
"response" => "Success", | |
"success" => true, | |
"data" => array() | |
); | |
// Make sure an action parameter has been sent to this endpoint | |
checkParams(array("action")); | |
switch($_REQUEST['action']){ | |
case "some_action": | |
output(); | |
break; | |
default: oops("Error: invalid action parameter"); | |
} | |
function checkParams($reqd){ | |
foreach($reqd as $param) | |
if(!isset($_REQUEST[$param])) | |
oops("Error: Missing $param parameter."); | |
} | |
function oops($oopsie){ | |
$GLOBALS['return']['response'] = $oopsie; | |
$GLOBALS['return']['success'] = false; | |
$GLOBALS['return']['data'] = array();; | |
output(); | |
} | |
function output(){ | |
header("Content-Type: application/json"); | |
echo json_encode($GLOBALS['return']); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment