Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created April 5, 2017 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pamblam/6152561979d51beb3bdad8498386bf09 to your computer and use it in GitHub Desktop.
Save Pamblam/6152561979d51beb3bdad8498386bf09 to your computer and use it in GitHub Desktop.
Simple starting point for PHP webservices
<?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