Building
RESTful Web Service
With PHP
Huy Nguyen - huy@byhuy.com

    REST and RESTful web service

    Building a RESTful framework with PHP
REpresentational State Transfer

    An architecture not a protocol or a standard

    Resource Oriented Approach

    World Wide Web is the key example
What is a RESTful web service?

   A web site for Machines!!!
MIME types
GET    POST
PUT DELETE HEAD   supported
   OPTIONS



Resources
                   URIs
     It's all about HTTP, dude!
REST vs RPC
     ROA vs SOA
RESOURCES vs FUNCTIONS
Flickr has a RESTful web service

        But it's crap!!!!!!!
This is RESTful:
http://feeds.feedburner.com/Geshansblog


and this is not:
http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value
RESTful Web Protocols

    Syndication protocols (RSS, Atom Feed)

    Atom Publishing Protocol

    RDF and OWL

    OpenID Single Sign-on Standard

    Google's OpenSocial Protocol

    Amazon's S3 Protocol

    ...
Now let's talk about PHP

... and how it supports REST
$_SERVER

 Accept => $_SERVER['HTTP_ACCEPT']

Request Method => $_SERVER['REQUEST_METHOD']
$_POST and $_GET



Everyone knows them!
RAW POST/PUT REQUEST
               DATA?

<?php

// get RAW HTTP POST
$rawData = file_get_contents('php://input');

// but this won't work with enctype="multipart/form-data" :(
Why is symfony not RESTful?



because RESTful web service is about
   resources, not functions/actions
My RESTful Framework

    Creating Resources

    Creating URIs

    Handling HTTP methods

    MIME-type support
Resources As Controllers



A resources is a class that handles HTTP methods
/**
   * HTTP GET handler
   */
  public function __handleGet()
  {
      // Not implemented
      $this->_response->setResponseCode(501);
  }

  /**
   * HTTP POST handler
   */
  public function __handlePost()
  {
      // Not implemented
      $this->_response->setResponseCode(501);
  }

  /**
   * HTTP PUT handler
   */
  public function __handlePut()
  {
      // Not implemented
      $this->_response->setResponseCode(501);
  }
/**
       * HTTP DELETE handler
       */
      public function __handleDelete()
      {
          // Not implemented
          $this->_response->setResponseCode(501);
      }

      /**
       * HTTP HEAD handler
       */
      public function __handleHead()
      {
          // Not implemented
          $this->_response->setResponseCode(501);
      }
Creating The URIs


/blog/([a-zA-Z0-9]+)/?   =>   blogResource
Composite Resources



  Konstrukt Framework
     (konstrukt.dk)
MIME Types & Multiple Views


         $_SERVER['HTTP_ACCEPT']


text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
My framework is called Kao
Thank you! I love PHP!
Any Question?

nguyenhainhathuy-building-restful-web-service

  • 1.
    Building RESTful Web Service WithPHP Huy Nguyen - huy@byhuy.com
  • 2.
    REST and RESTful web service  Building a RESTful framework with PHP
  • 3.
    REpresentational State Transfer  An architecture not a protocol or a standard  Resource Oriented Approach  World Wide Web is the key example
  • 4.
    What is aRESTful web service? A web site for Machines!!!
  • 5.
    MIME types GET POST PUT DELETE HEAD supported OPTIONS Resources URIs It's all about HTTP, dude!
  • 6.
    REST vs RPC ROA vs SOA RESOURCES vs FUNCTIONS
  • 7.
    Flickr has aRESTful web service But it's crap!!!!!!!
  • 8.
    This is RESTful: http://feeds.feedburner.com/Geshansblog andthis is not: http://api.flickr.com/services/rest/?method=flickr.test.echo&name=value
  • 9.
    RESTful Web Protocols  Syndication protocols (RSS, Atom Feed)  Atom Publishing Protocol  RDF and OWL  OpenID Single Sign-on Standard  Google's OpenSocial Protocol  Amazon's S3 Protocol  ...
  • 10.
    Now let's talkabout PHP ... and how it supports REST
  • 11.
    $_SERVER Accept =>$_SERVER['HTTP_ACCEPT'] Request Method => $_SERVER['REQUEST_METHOD']
  • 12.
  • 13.
    RAW POST/PUT REQUEST DATA? <?php // get RAW HTTP POST $rawData = file_get_contents('php://input'); // but this won't work with enctype="multipart/form-data" :(
  • 14.
    Why is symfonynot RESTful? because RESTful web service is about resources, not functions/actions
  • 15.
    My RESTful Framework  Creating Resources  Creating URIs  Handling HTTP methods  MIME-type support
  • 16.
    Resources As Controllers Aresources is a class that handles HTTP methods
  • 17.
    /** * HTTP GET handler */ public function __handleGet() { // Not implemented $this->_response->setResponseCode(501); } /** * HTTP POST handler */ public function __handlePost() { // Not implemented $this->_response->setResponseCode(501); } /** * HTTP PUT handler */ public function __handlePut() { // Not implemented $this->_response->setResponseCode(501); }
  • 18.
    /** * HTTP DELETE handler */ public function __handleDelete() { // Not implemented $this->_response->setResponseCode(501); } /** * HTTP HEAD handler */ public function __handleHead() { // Not implemented $this->_response->setResponseCode(501); }
  • 19.
  • 20.
    Composite Resources Konstrukt Framework (konstrukt.dk)
  • 22.
    MIME Types &Multiple Views $_SERVER['HTTP_ACCEPT'] text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  • 23.
    My framework iscalled Kao
  • 24.
    Thank you! Ilove PHP!
  • 25.