PHP Web Sercvices REST

REST
Representational State Transfer, or REST, is aWeb service architectural style in which the focus is on the presence of resources in the system. Each resourcemust be identified by a global identifier—a URI. To access these resources, clients communicate with the REST service by HTTP, and the server responds with a representation of the resource. This representation is often in the formof HTML or XML. Services that use the REST architecture are referred to as RESTful services; those who use or provide RESTful services are sometimes humorously referred to as RESTafarians.

There are a number of RESTful Web services, the most popular of which thrive in the blogosphere. In a loose sense,Web sites that provide RSS and RDF feeds provide a RESTful service. Loosening the definition even more reveals that the entire Web itself may be thought of as following a RESTful architecture with myriad resources and only a few actions to interact with them: GET, POST, PUT, HEAD, etc. In general, however, RESTfulWeb services allow standard GET requests to a resource and, in return, send an XML response. These services are not discoverable, so most providers
have well-documented APIs.

Since RESTfulWeb services are not discoverable, do not provide a WSDL, and have no common interface for communication, there is no one REST class provided in PHP to access all RESTful services; however, most RESTful services respond with XML data, and SimpleXML provides the best interface to interact with them. The popular bookmarking site, del.icio.us, is one example of aWeb site providing a REST service that returns XML data ready for SimpleXML to consume.
In the following example, the request made to api.del.icio.us requests all bookmarks tagged with the keyword foo:

$u = ’username’;
$p = ’password’;
$fooTag = "https://{$u}:{$p}@api.del.icio.us/v1/posts/all?tag=foo";
$bookmarks = new SimpleXMLElement($fooTag, NULL, true);
foreach ($bookmarks->post as $bookmark)
{
echo ’’;
echo htmlentities($bookmark[’description’]);
echo "

\n";
}


The URI stored in $fooTag is the resource identifier for the data retrieved. SimpleXML handles the request and conversion of the received XML data into an object. Note that del.icio.us uses HTTP authentication over SSL for its REST URIs; most RESTful services provide some kind of authentication or developer key scheme to gain access to the service.

Share this

0 Comment to " PHP Web Sercvices REST "

Post a Comment