phpandwebservices-100223023906-phpapp01

download phpandwebservices-100223023906-phpapp01

of 55

Transcript of phpandwebservices-100223023906-phpapp01

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    1/55

    PHP and Web Services:

    Perfect Partners

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    2/55

    About Me

    Lorna Jane Mitchell

    PHP Developer/Trainer/Consultant at Ibuildings

    Hosting DPC

    Editor at techPortal

    http://lornajane.net

    @lornajane

    http://lornajane.net/http://lornajane.net/
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    3/55

    PHP

    PHP: "solving the web problem"

    The problem evolves

    PHP evolves

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    4/55

    PHP

    Server

    Client

    Web application tool

    Command line tool

    GTK ...

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    5/55

    PHP

    Ubiquitous

    Runs on your platform *nix

    mac

    windows (yes really, rather well!)

    Can talk to your other systems

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    6/55

    Web Services

    "scalable, reusable libraries"

    Interface over HTTP

    Modular application design

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    7/55

    Architecture

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    8/55

    Traditional Architecture

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    9/55

    Traditional Architecture

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    10/55

    Additional Channels

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    11/55

    Services Architecture

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    12/55

    Inside The Service Layer

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    13/55

    Data Formats

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    14/55

    JSON

    JavaScript Object Notation

    Natively read/write in most languages

    Very simple! (we like simple)

    Limitations

    no data typing

    no distinction between object and array

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    15/55

    Writing JSON from PHP

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    16/55

    Reading JSON from PHP

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    17/55

    Reading JSON from PHP

    stdClass Object(

    [starter] => Array(

    [0] => prawn cocktail[1] => soup of the day

    )[main course] => Array

    ([0] => roast chicken[1] => fish 'n' chips[2] => macaroni cheese

    )

    [pudding] => Array(

    [0] => cheesecake[1] => treacle sponge

    )

    )

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    18/55

    XML

    eXtensible Markup Language

    Familiar

    Can give more detail than JSON

    Native read/write in most languages

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    19/55

    Working with XML from PHP

    Lots of options

    SimpleXML

    DOM

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    20/55

    SimpleXML Example

    1 5 6 Lunch7 Dinner

    8 Dessert9 Drinks10 11 XML;1213 $simplexml=newSimpleXMLElement($xml);14 var_dump($simplexml);

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    21/55

    SimpleXML Example

    object(SimpleXMLElement)#1 (1) {["menu"]=>array(5) {[0]=>string(5) "Lunch"[1]=>string(6) "Dinner"[2]=>string(7) "Dessert"[3]=>string(6) "Drinks"

    }}

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    22/55

    SimpleXML Example

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    23/55

    SimpleXML Example

    LunchDinnerDessert

    Drinks

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    24/55

    Service Types

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    25/55

    Service Types

    *-RPC

    XML-RPC

    JSON-RPC

    SOAP

    REST

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    26/55

    RPC

    All URLs point to a single endpoint

    Parameters give method names

    Request body can take a variety of formats

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    27/55

    Example RPC services

    Using Flickr's XML-RPC

    Test method: just echoes back to user

    XML formatted data

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    28/55

    Flickr Echo Example: XML

    flickr.test.echo

    api_key ....

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    29/55

    RPC from PHP: curl

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    30/55

    RPC from PHP: pecl_http

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    31/55

    Flickr Response

    54rt346

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    32/55

    RPC Advantages

    RPC is a great format for wrapping existing

    functionality

    Can abstract between existing systems

    Familiar functional paradigm

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    33/55

    Delivering RPC

    Consumers will need

    Service URL

    Docs of functions and arguments

    If this was an existing system, existing docs may

    suffice

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    34/55

    Wrapping RPC

    RPC is a library-like interface

    Can easily wrap existing libraries to call like this

    Can wrap an interface to an RPC service to

    look like a library

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    35/55

    Wrapping RPC Example

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    36/55

    SOAP

    Special case of RPC using XML

    Has given formats for messages and errors

    Libraries exist for creating server and client in

    most languages

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    37/55

    PHP SOAP Server Example

    require_once('lib/myClass.php');

    $server = new SoapServer("service.wsdl");

    $server->setClass("MyClass");

    $server->handle();

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    38/55

    PHP SOAP Client Example

    $wsdl = "Service.wsdl";

    $client = new SoapClient($wsdl, $params);

    $output = $client->requestShot(

    'http://www.php.net','', 300, 400);

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    39/55

    WSDL

    Web Service Description Language

    Widely used with SOAP

    Describes the methods, arguments and data

    types available

    IDEs can read this and hint

    Validity of requests is checked before they are

    sent

    S

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    40/55

    WSDL

    D li i SOAP

    http://schemas.xmlsoap.org/wsdl/soap/http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchemahttp://schemas.xmlsoap.org/soap/encoding/http://schemas.xmlsoap.org/soap/encoding/http://schemas.xmlsoap.org/wsdl/http://schemas.xmlsoap.org/wsdl/http://schemas.xmlsoap.org/wsdl/http://schemas.xmlsoap.org/soap/encoding/http://www.w3.org/2001/XMLSchemahttp://schemas.xmlsoap.org/wsdl/soap/
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    41/55

    Delivering SOAP

    In WSDL mode, only the WSDL needs to be

    supplied

    Otherwise method names, arguments and

    types will be needed

    REST

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    42/55

    REST

    A series of concepts

    Generally uses HTTP (HyperText Transfer

    Protocol)

    URLs are resource locations

    Verbs tell the service what to do

    Status codes indicate what the outcome was

    I l ti REST

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    43/55

    Implementing REST

    Standard application architecture

    Routing to map requests to internal functionality

    Output not always HTML

    REST CRUD

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    44/55

    REST CRUD

    Action HTTP Verb

    Retrieve GET

    Create POST

    Update PUT

    Delete DELETE

    REST E l

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    45/55

    REST Examples

    GET

    http://localhost/users

    http://localhost/users/harry

    POST

    http://localhost/users PUT

    http://localhost/users/harry

    REST from PHP GET

    http://localhost/usershttp://localhost/users/harryhttp://localhost/usershttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/usershttp://localhost/users/harryhttp://localhost/usershttp://localhost/users/harry
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    46/55

    REST from PHP: GET

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    47/55

    REST from PHP: GET

    Health Warning!

    curl will echo output

    use CURLOPT_RETURNTRANSFER to capture it

    instead

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    48/55

    REST from PHP: POST

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    49/55

    REST from PHP: PUT

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    50/55

    REST from PHP: DELETE

    1

  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    51/55

    Delivering REST

    Full documentation including URL formats, data

    types, and response formats

    Must include information about error handling

    REST as an inspiration

    http://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harry
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    52/55

    REST as an inspiration

    RESTful is a strict definition

    REST is full of great ideas

    REST is great for clean, simple, robust services

    Cherry-pick the bits that work

    Just don't call it "RESTful" :)

    Resources

    http://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harry
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    53/55

    Resources

    RESTful Web Services Leonard Richardson

    and Sam Ruby

    http://php.net

    http://benramsey.com

    http://lornajane.net

    http://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://localhost/users/harryhttp://php.net/http://benramsey.com/http://lornajane.net/http://lornajane.net/http://benramsey.com/http://php.net/
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    54/55

    Questions ???

    Thankyou

    http://lornajane.net/http://lornajane.net/http://lornajane.net/
  • 8/2/2019 phpandwebservices-100223023906-phpapp01

    55/55

    Thankyou

    Lorna Jane Mitchell

    http://ibuildings.com

    http://lornajane.net

    @lornajane

    http://slideshare.net/lornajane

    http://lornajane.net/http://lornajane.net/http://lornajane.net/http://lornajane.net/http://lornajane.net/http://ibuildings.com/http://lornajane.net/http://lornajane.net/http://ibuildings.com/