Skip to main content

RESTApi

  • REST(Representational State Transfer): a set of rules on how to structure an application

  • A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data

  • That data can be used to GET, PUT, POST and DELETE data types, which refers to the read, update, insert and delete php_rest

  • Using RESTful API to request a list of gists from github

  • Using RESTful API to request a particular gist from github

  • URL Rewrite

  • Front Controller

Using RESTful API to request a list of gists from github

<?
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL="https://api.github.com/gists", //setting url as a option
CURLOPT_RETURNTRANSFER => true, //we want return response as a string
CURLOPT_CUSTOMREQUEST => "GET", //default method is GET so we do not need to specify it
CURLOPT_USERAGENT => "weili0505" //github requires the user agent string
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response,true); //decode json response into associative array
foreach ($data as $gist) { //loop through each gist and print out id and description (default number is 30 per page)
echo $gist["id"], " - ", $gist["description"], "<br>";
}
?>

Using RESTful API to request a particular gist from github

<?
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL="https://api.github.com/gists/337012b9e9c1b8a0efd3aed45f01bc42", //setting url as a option
CURLOPT_RETURNTRANSFER => true, //we want return response as a string
CURLOPT_CUSTOMREQUEST => "GET", //default method is GET so we do not need to specify it
CURLOPT_USERAGENT => "weili0505" //github requires the user agent string
]);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response,true); //decode json response into associative array
eco $data;
?>

URL Rewrite

  • RESTful API require access points to look like /tasks and /tasks/123, but there is no folder called tasks or file task/123
  • We can change this by using URL Rewriting capabilities of of the web server php_rewrite
  • In apache server, we can do url rewrite by creating a .htaccess file
RewriteEngine On
#ignore rewrite for any existing files(such as images, documents), directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
#create a rewrite rule that says any url to run the index.php script, [L]tells the rewrite engine to stop processing
RewriteRule . index.php [L]

Front Controller

  • Now every request goes through index.php script, it is our front controller
  • We can process the url to get the resource and id
  • We can also get the method
<?
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); //get the path from the request without any ?page=1 at the end
$parts = explode("/", $path);
$resource = $parts[2];
$id = $parts[3];
$method = $_SERVER["REQUEST_METHOD"];
echo $resource, $id, $method;
?>

Set HTTP Status Codes

  • Now every url request is valid, and will go through index.php script
  • We want to only allow certain requests and deny all others
<?
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); //get the path from the request without any ?page=1 at the end
$parts = explode("/", $path);
$resource = $parts[2];
$id = $parts[3];
$method = $_SERVER["REQUEST_METHOD"];
echo $resource, $id, $method;

echo $resource, $id, $method;
if($resource != "tasks"){ //if the resource request is not what we specify
echo "not tasks";
http_response_code(404); //we response with a 404 status code
//header("HTTP 1.1 404 Not Found"); //same as above
exit;
}
?>