Created
September 29, 2011 00:27
-
-
Save Nub/1249688 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 | |
| /* | |
| ZTAPI by Zachry Buddy Thayer | |
| Version 1.0 | |
| License: CC BY-SA (Crreative Commons Share Alike) | |
| */ | |
| class ZTAPI{ | |
| private $methods = array(); | |
| private $data = ""; | |
| private $apiCall = ""; | |
| constant APIKEY_KEY = "APIKey"; | |
| constant METHOD_KEY = "Action"; | |
| constant DATA_KEY = "Data"; | |
| constant ERROR_INVALID_APIKEY = "INVALID_APIKEY"; | |
| constant ERROR_METHOD_NOT_FOUND = "METHOD_NOT_FOUND"; | |
| constant ERROR_INVALID_DATA = "INVALID_DATA"; | |
| constant ERROR_DATA_NOT_FOUND = "DATA_NOT_FOUND"; | |
| constant ERROR_JSON_ENCODE_FAILED = "JSON_ENCODE_FAILED"; | |
| constant ERROR_JSON_DECODE_FAILED = "JSON_DECODE_FAILED"; | |
| constant ERROR_BAD_API_CALL = "BAD_API_CALL"; | |
| constant ERROR_API_CALL_FAILED = "API_CALL_FAILED"; | |
| /* | |
| Append method to array of methods | |
| */ | |
| function addMethod(&$method){ | |
| $method->setParent($this);//Link to parent | |
| $this->methods[$method->name()] = $method; | |
| } | |
| /* | |
| Checks APIKey returns BOOLEAN | |
| */ | |
| function validateAPIKey($APIKey){ | |
| return TRUE; | |
| } | |
| function throwError($error){ | |
| $errorObject = Array( | |
| 'Error':$error, | |
| 'APICall':$this->apiCall | |
| ); | |
| $errorJSON = json_encode($errorObject); | |
| if(!isset($errorJSON)){ | |
| die('{"Error":"JSON_ENCODING_FAILED_IN_ERROR_THROW", "APICall":""}'); | |
| } | |
| die($errorJSON); | |
| } | |
| function run(){ | |
| $this->apiCall = @file_get_contents('php://input'); | |
| if(!isset($this->apiCall)) | |
| $this-throwError(ZTAPI::ERROR_BAD_API_CALL); | |
| $JSONBody = json_decode($this->apiCall); | |
| if(!isset($JSONBody)) | |
| $this-throwError(ZTAPI::ERROR_JSON_DECODE_FAILED); | |
| if(!validAPIKey($JSONBody[ZTAPI::APIKEY_KEY])){ | |
| $this->throwError(ZTAPI::ERROR_INVALID_APIKEY); | |
| } | |
| $method = $JSONBody[ZTAPI::METHOD_KEY]; | |
| if(!isset($method) || isset($this->methods[$method])) | |
| $this-throwError(ZTAPI::ERROR_METHOD_NOT_FOUND); | |
| $data = $JSONBody[ZTAPI::DATA_KEY]; | |
| if(!isset($data) || isset($this->methods[$data])) | |
| $this-throwError(ZTAPI::ERROR_DATA_NOT_FOUND); | |
| // Run API method call | |
| $output = $this->methods[$method]->invoke($data); | |
| if(!isset($output)) | |
| $this-throwError(ZTAPI::ERROR_API_CALL_FAILED); | |
| $JSONOutput = json_encode($output); | |
| if(!isset($JSONOutput)) | |
| $this-throwError(ZTAPI::ERROR_JSON_ENCODE_FAILED); | |
| echo($JSONOutput); | |
| } | |
| } | |
| abstract class ZTAPIMethod{ | |
| abstract public function __construct(); | |
| /* | |
| refrence to parent for possible method use | |
| */ | |
| protected $_parent; | |
| /* | |
| Set parent | |
| */ | |
| function setParent(&$parent){ | |
| $this->_parent = $parent; | |
| } | |
| /* | |
| Method to be called when refrence is found | |
| $data: should be a data array/keypair refrence (ZTAPI::DATA_KEY object is passed) | |
| Returns: array/keypair object to be json_encoded | |
| */ | |
| abstract public function invoke($data); | |
| /* | |
| Method Name | |
| /api/methodName/ | |
| */ | |
| protected $_name; | |
| /* | |
| Fetch name | |
| */ | |
| function name(){ | |
| return $this->_name; | |
| } | |
| //Below is optional, just extraneous information, but could be useful on large projects | |
| /* | |
| String version of plugin | |
| */ | |
| protected $_version; | |
| /* | |
| Fetch version | |
| */ | |
| function version(){ | |
| return $this->_version; | |
| } | |
| function __toString(){ | |
| return "Method: ".$this->_name." Version: ".$this->_version; | |
| } | |
| } | |
| /* | |
| Example method that lists the args fed to it. | |
| */ | |
| class ZTAPITestMethod extends ZTAPIMethod{ | |
| function invoke($args){ | |
| $ret = ""; | |
| foreach($args => $arg){ | |
| $ret .= "<b>".$arg."</b></br>"; | |
| } | |
| return $ret; | |
| } | |
| public function __construct(){ | |
| $this->_name = "ZTAPITestMethod"; | |
| $this->_version = "1.0"; | |
| } | |
| } | |
| //Example use | |
| $testMethod = new ZTAPITestMethod(); | |
| $myAPI = new ZTAPI(); | |
| //Register API methods | |
| $myAPI->addMethod($testMethod); | |
| $myAPI->run(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment