Showing posts with label APIs. Show all posts
Showing posts with label APIs. Show all posts

Oct 11, 2016

How to write RESTful APIs in "Vanilla" PHP?

PHP supports them native. In a certain PHP, I have to code in "pure PHP", no RESTful APIs library/engine provided. So I decided to write a file called api.php to handle all API request. I used this pattern
if ($grabber->method() === 'GET') {
    // define your APIs here
    echo json_encode(array('Appl RESTful APIs'), JSON_UNESCAPED_UNICODE);
    exit(0);

} else if ($grabber->method() === 'POST') {
    // define your APIs here

} else if ($grabber->method() === 'PUT') {
    // define your APIs here

} else if ($grabber->method() === 'DELETE') {
    // define your APIs here

}
You can see the api.php at Github