How to create web API service in PHP [duplicate] How to create web API service in PHP [duplicate] php php

How to create web API service in PHP [duplicate]


Basically, What is an API?

If you understand how data goes from a HTML form to php code, then trust me you know all about API's. Here rather than talking forms, we talk about urls that are created at the end by those forms.

What are the Types of API?

There are two types of heavily used APIs for web services: SOAP and REST. Google is one of the major players with a SOAP based API while Yahoo (and most of their recent acquisitions) have taken the REST approach. More often than not, a “Web 2.0” service you come across today will probably be using REST.

How to create a REST API in PHP with Authentication Key to delete a value from database?

Let’s say we have a PHP class (manage.php) that helps us manage entries in a database:

class manage {    private $entryId;    function __construct($entryId) {        $this->entryId = $entryId;    }    function deleteEntry() {        //delete $this->entryId from database    }}

On our own server, we might access this functionality like so:

require_once('manage.php');$m = new manage(23);$m->deleteEntry();

Easy enough for us, but how do we allow someone not on our server access to the same functionality? For that, we’ll create a third file to act as a buffer (or “interface”) between other developers and our class. Here’s an example of a file we might create to allow developers to access the delete function in our class, we’ll locate it at ‘api/delete.php’

require_once('manage.php');if (hasPermission($_POST['api_key']) {    $m = new manage($_POST['entry_id']);    $m->deleteEntry();}

This will allow users to send a POST request to us at http://example.com/api/delete.php with an api_key and an entry_id. You’ll notice the function is very similar to what we wrote on our own server except we check the POST api_key variable using a function to see if its authorized to access the database. We didn’t include that function here (hasPermission) for simplicity’s sake. In addition to what’s shown, you’d also have to find the user’s account based off of the api_key, put in some error checking and (if you want to make a good API) provide a properly formatted success or error response after the request. We’ll get into the success and error responses in a bit.

How To Design A Good API and Why it Matters?

I guess this video by Google can explain lot better than me. http://youtu.be/aAb7hSCtvGw

References:

http://en.wikipedia.org/wiki/Representational_state_transfer

http://en.wikipedia.org/wiki/SOAP

http://particletree.com/features/how-to-add-an-api-to-your-web-service/

http://www.webresourcesdepot.com/how-to-create-an-api-10-tutorials/

Google Search Result

Note: The answer is compilation of all references!!.