Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create web API service in PHP [duplicate]

Tags:

Possible Duplicate:
Is it necessary to have a wsdl file for the creation of a webservice in php?

I want to create a web API.

My requirement is that I want to create to a way so that my customers would be able to insert/delete data from my website database, but I do not want the customers directly logged into my site. I want them to call my WSDL file URL via a client request, which will return the response to the customer.

I have heard that we can do so with the help of SOAP call or NuSOAP. I have tried several methods after doing some googling but have no success.

Can you please tell me the step by step instructions how I can achieve this.

like image 643
ashokostech Avatar asked Dec 01 '11 12:12

ashokostech


1 Answers

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!!.

like image 55
4 revs, 3 users 88% Avatar answered Sep 27 '22 16:09

4 revs, 3 users 88%