Created
April 5, 2015 10:19
-
-
Save AnowarCST/f7599e7f6de680031dbe to your computer and use it in GitHub Desktop.
Making a test API
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 | |
require_once './API.class.php'; | |
class MyAPI extends API | |
{ | |
protected $User; | |
public function __construct($request, $origin) { | |
parent::__construct($request); | |
// Abstracted out for example | |
$APIKey = 'apiKey'; | |
$User = 'anowar'; | |
if (!array_key_exists('apiKey', $this->request)) { | |
throw new Exception('No API Key provided'); | |
} else if (!$APIKey == $this->request['apiKey']) { | |
throw new Exception('Invalid API Key'); | |
} else if (array_key_exists('token', $this->request) && | |
!$User->get('token', $this->request['token'])) { | |
throw new Exception('Invalid User Token'); | |
} | |
$this->User = $User; | |
} | |
/** | |
* Example of an Endpoint | |
*/ | |
protected function example() { | |
if ($this->method == 'GET') { | |
return "Your name is " . $this->User->name; | |
} else { | |
return "Only accepts GET requests"; | |
} | |
} | |
} | |
// Requests from the same server don't have a HTTP_ORIGIN header | |
//if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) { | |
// $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME']; | |
//} | |
//echo json_encode($_POST); | |
try { | |
$API = new MyAPI('apiKey/apiKey', $_REQUEST); | |
echo $API->processAPI(); | |
} catch (Exception $e) { | |
echo json_encode(Array('error' => $e->getMessage())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment