Monday, November 11, 2013

PHP REST SERVER

--


PHP REST SERVER

REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.
REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.
  • In many ways, the World Wide Web itself, based on HTTP, can be viewed as a REST-based architecture.
RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.
REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.). Later, we will see how much more simple REST is.
  • Despite being simple, REST is fully-featured; there's basically nothing you can do in Web Services that can't be done with a RESTful architecture.
REST is not a "standard". There will never be a W3C recommendataion for REST, for example. And while there are REST programming frameworks, working with REST is so simple that you can often "roll your own" with standard library features in languages like Perl, Java, or C#.


filename: RestServer.php
<?php
 class RestServer
   {
       public $serviceClass;
   
       public function __construct($serviceClass)
       {
           $this->serviceClass = $serviceClass;
       }
   
       public function handle()
       {
           if (array_key_exists("method", array_change_key_case($_REQUEST, CASE_LOWER)))
           {
               $rArray = array_change_key_case($_REQUEST, CASE_LOWER);
               $method = $rArray["method"];
   
               if (method_exists($this->serviceClass, $method))
               {
                   $ref = new ReflectionMethod($this->serviceClass, $method);
                   $params = $ref->getParameters();
                   $pCount = count($params);
                   $pArray = array();
                   $paramStr = "";
                   
                   $i = 0;
                   
                   foreach ($params as $param)
                   {
                       $pArray[strtolower($param->getName())] = null;
                       $paramStr .= $param->getName();
                       if ($i != $pCount-1)
                       {
                           $paramStr .= ", ";
                       }
                       
                       $i++;
                   }
                   foreach ($pArray as $key => $val)
                   {
                       $pArray[strtolower($key)] = $rArray[strtolower($key)];
                   }
   
                   if (count($pArray) == $pCount && !in_array(null, $pArray))
                   {
                       echo call_user_func_array(array($this->serviceClass, $method), $pArray);
                   }
                   else
                   {
                       echo json_encode(array('error' => "Required parameter(s) for ". $method .": ". $paramStr));
                   }
               }
               else
               {
                   echo json_encode(array('error' => "The method " . $method . " does not exist."));
               }
           }
           else
           {
               echo json_encode(array('error' => 'No method was requested.'));
           }
       }
   }
?>





filename: example.php

<?php
 // example request: http://path/to/resource/example.php?method=sayHello&name=World
 require_once "RestServer.php";
 $rest = new RestServer('Hello');
 $rest->handle();
 class Hello
 {
    public static function sayHello($name)
    {
     return "Hello, " . $name;
    }
 }
?>



1 comment:


  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    PHP Developer Software in India

    ReplyDelete

Labels