SlideShare uma empresa Scribd logo
1 de 55
Baixar para ler offline
PHP and Web Services:
   Perfect Partners
About Me
●
    Lorna Jane Mitchell
●
    PHP Developer/Trainer/Consultant at Ibuildings
    ●
        Hosting DPC
    ●
        Editor at techPortal

●
    http://lornajane.net
●
    @lornajane
PHP
●
    PHP: "solving the web problem"
●
    The problem evolves
●
    PHP evolves
PHP
●
    Server
●
    Client
●
    Web application tool
●
    Command line tool
●
    GTK ...
PHP
●
    Ubiquitous
●
    Runs on your platform
    ●
        *nix
    ●
        mac
    ●
        windows (yes really, rather well!)

●
    Can talk to your other systems
Web Services
●
    "scalable, reusable libraries"
●
    Interface over HTTP
●
    Modular application design
Architecture
Traditional Architecture
Traditional Architecture
Additional Channels
Services Architecture
Inside The Service Layer
Data Formats
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
Writing JSON from PHP
   1   <?php
   2
   3   $menu['starter'] = array( "prawn cocktail",
   4                             "soup of the day");
   5   $menu['main course'] = array( "roast chicken",
   6                                 "fish 'n' chips",
   7                                 "macaroni cheese");
   8   $menu['pudding'] = array( "cheesecake",
   9                             "treacle sponge");
  10
  11   echo json_encode($menu);




{"starter":["prawn cocktail","soup of the day"],"main 
course":["roast chicken","fish 'n' chips","macaroni 
cheese"],"pudding":["cheesecake","treacle sponge"]}
Reading JSON from PHP
    1 <?php
    2
    3 $json = '{"starter":["prawn cocktail","soup of the
day"],"main course":["roast chicken","fish 'n'
chips","macaroni cheese"],"pudding":["cheesecake","treacle
sponge"]}';
    4
    5 print_r(json_decode($json));
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
        )
)
XML
●
    eXtensible Markup Language
●
    Familiar
●
    Can give more detail than JSON
●
    Native read/write in most languages
Working with XML from PHP
●
    Lots of options
●
    SimpleXML
●
    DOM
SimpleXML Example
 1   <?php
 2
 3   $xml = <<< XML
 4   <?xml version="1.0" ?>
 5   <menus>
 6        <menu>Lunch</menu>
 7        <menu>Dinner</menu>
 8        <menu>Dessert</menu>
 9        <menu>Drinks</menu>
10   </menus>
11   XML;
12
13   $simplexml = new SimpleXMLElement($xml);
14   var_dump($simplexml);
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"
  }
}
SimpleXML Example
    1 <?php
    2
    3 $simplexml = simplexml_load_string('<?xml version="1.0"
?><menus/>');
    4 $simplexml->addChild('menu','Lunch');
    5 $simplexml->addChild('menu','Dinner');
    6 $simplexml->addChild('menu','Drinks');
    7 $simplexml->addChild('menu','Dessert');
    8
    9 echo $simplexml->asXML();
SimpleXML Example

<?xml version="1.0"?>
<menus>
    <menu>Lunch</menu>
    <menu>Dinner</menu>
    <menu>Dessert</menu>
    <menu>Drinks</menu>
</menus>
Service Types
Service Types
●
    *-RPC
    – XML-RPC

    – JSON-RPC

●
    SOAP
●
    REST
RPC
●
    All URLs point to a single endpoint
●
    Parameters give method names
●
    Request body can take a variety of formats
Example RPC services
●
    Using Flickr's XML-RPC
●
    Test method: just echoes back to user
●
    XML formatted data
Flickr Echo Example: XML
<?xml version="1.0"?>
<methodCall>
  <methodName>flickr.test.echo</methodName>
  <params>
    <param>
      <value>
        <struct>
           <member>
             <name>api_key</name>
             <value>....</value>
           </member>
        </struct>
      </value>
    </param>
  </params>
</methodCall>
RPC from PHP: curl
 1   <?php
 2
 3   // $xml is existing SimpleXMLElement Object
 4   $url = 'http://api.flickr.com/services/xmlrpc/';
 5   $ch = curl_init($url);
 6
 7   curl_setopt($ch, CURLOPT_POST, 1);
 8   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml->asXML());
 9
10   $response = curl_exec($ch);
11   curl_close($ch);
RPC from PHP: pecl_http
 1   <?php
 2
 3   $url = 'http://api.flickr.com/services/xmlrpc/';
 4
 5   // procedural method
 6   $response = http_post_data($url, $xml->asXML());
 7
 8   // alternative method
 9   $request = new HTTPRequest($url, HTTP_METH_POST);
10   $request->setRawPostData($xml->asXML());
11   $request->send();
12   $response = $request->getResponseBody();
13
14   var_dump($response);
Flickr Response
<?xml version="1.0" encoding="utf-8" ?>
<methodResponse>
  <params>
    <param>
      <value>
        <string>&lt;api_key&gt;54rt346&lt;/api_key&gt;
        </string>
      </value>
    </param>
  </params>
</methodResponse>
RPC Advantages
●
    RPC is a great format for wrapping existing
    functionality
●
    Can abstract between existing systems
●
    Familiar functional paradigm
Delivering RPC
●
    Consumers will need
    – Service URL

    – Docs of functions and arguments

    – If this was an existing system, existing docs may
     suffice
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
Wrapping RPC Example
 1   <?php
 2
 3   class Handler
 4   {
 5    function __call($method, $args) {
 6       $ch = curl_init('http://localhost');
 7       $data['method'] = $method;
 8       foreach($args as $a) $data[$a] = $a;
 9
10      curl_setopt($ch, CURLOPT_POST,1);
11      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
12      $response = curl_exec($ch); curl_close($ch);
13
14      var_dump($response);
15    }
16   }
17   $h = new Handler();
18   $h->dance('cat','dog','rabbit','penguin');
19
SOAP
●
    Special case of RPC using XML
●
    Has given formats for messages and errors
●
    Libraries exist for creating server and client in
    most languages
PHP SOAP Server Example

require_once('lib/myClass.php');


$server = new SoapServer("service.wsdl");
$server->setClass("MyClass");
$server->handle();
PHP SOAP Client Example

$wsdl = "Service.wsdl";
$client = new SoapClient($wsdl, $params);


$output = $client->requestShot(
    'http://www.php.net','', 300, 400);
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
WSDL
<?xml version ='1.0' encoding ='UTF-8' ?>
 <definitions name='MyClass'      targetNamespace='urn:MyClassInventory'     xmlns:tns='urn:MyClassInventory'    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'   xmlns:xsd='http://www.w3.org/2001/XMLSchema'   xmlns:soapenc='
http://schemas.xmlsoap.org/soap/encoding/'      xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'   xmlns='http://schemas.xmlsoap.org/wsdl/'>
 <message name='getAccountStatusRequest'>
   <part name='accountID' type='xsd:string'/>
 </message>
 <message name='getAccountStatusResponse'>
   <part name='accountID' type='xsd:string'/>
   <part name='counter' type='xsd:float' />
 </message>
 <portType name='MyClassPortType'>
   <operation name='getAccountStatus'>
    <input message='tns:getAccountStatusRequest'/>
    <output message='tns:getAccountStatusResponse'/>
   </operation>
 </portType>
 <binding name='MyClassBinding' type='tns:MyClassPortType'>
   <soap:binding style='rpc'
    transport='http://schemas.xmlsoap.org/soap/http'/>
   <operation name='getAccountStatus'>
    <soap:operation soapAction='urn:xmethods-delayed-quotes#getAccountStatus'/>
    <input>
     <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'             encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </input>
    <output>
     <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
      encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </output>
   </operation>
 </binding>
 <service name='MyClassService'>
   <port name='MyClassPort' binding='tns:MyClassBinding'>
    <soap:address location='http://rivendell.local:10002/MyClassServiceServer.php'/>
   </port>
 </service>
 </definitions>
Delivering SOAP
●
    In WSDL mode, only the WSDL needs to be
    supplied
●
    Otherwise method names, arguments and
    types will be needed
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
Implementing REST
●
    Standard application architecture
●
    Routing to map requests to internal functionality
●
    Output not always HTML
REST CRUD
Action     HTTP Verb


Retrieve   GET

Create     POST

Update     PUT

Delete     DELETE
REST Examples
●
    GET
    ●
        http://localhost/users
    ●
        http://localhost/users/harry

●
    POST
    ●
        http://localhost/users

●
    PUT
    ●
        http://localhost/users/harry
REST from PHP: GET
1 <?php
2
3 $result = file_get_contents('http://localhost/users');
4 var_dump($result);
REST from PHP: GET
    1 <?php
    2
    3 $ch = curl_init('http://localhost/users');
    4
    5 curl_exec($ch);




●
    Health Warning!
    ●
        curl will echo output
    ●
        use CURLOPT_RETURNTRANSFER to capture it
        instead
REST from PHP: POST
 1   <?php
 2
 3   $ch = curl_init('http://localhost/users');
 4
 5   $data = array ("name" => "Cho Chang",
 6               "house" => "Ravenclaw");
 7
 8   curl_setopt($ch, CURLOPT_POST, 1);
 9   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
10
11   curl_exec($ch);
REST from PHP: PUT
 1   <?php
 2
 3   $ch = curl_init('http://localhost/users/cho');
 4
 5   $data = array ("name" => "Cho Chang",
 6               "house" => "Ravenclaw"
 7               "age" => 15);
 8
 9   curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
10   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
11
12   curl_exec($ch);
13
REST from PHP: DELETE
1   <?php
2
3   $ch = curl_init('http://localhost/users/ginny');
4
5   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
6
7   curl_exec($ch);
Delivering REST
●
    Full documentation including URL formats, data
    types, and response formats
●
    Must include information about error handling
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
●
    RESTful Web Services – Leonard Richardson
    and Sam Ruby
●
    http://php.net
●
    http://benramsey.com
●
    http://lornajane.net
Questions ???
Thankyou
●
    Lorna Jane Mitchell
●
    http://ibuildings.com
●
    http://lornajane.net
●
    @lornajane



●
    http://slideshare.net/lornajane

Mais conteúdo relacionado

Mais procurados

Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHPZoran Jeremic
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATIONPHP BASIC PRESENTATION
PHP BASIC PRESENTATIONkrutitrivedi
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit universityMandakini Kumari
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3Bradley Holt
 
Services web RESTful
Services web RESTfulServices web RESTful
Services web RESTfulgoldoraf
 
Common Gateway Interface
Common Gateway InterfaceCommon Gateway Interface
Common Gateway InterfaceBalu Masulkar
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4Gheyath M. Othman
 
Crud operations using aws dynamo db with flask ap is and boto3
Crud operations using aws dynamo db with flask ap is and boto3Crud operations using aws dynamo db with flask ap is and boto3
Crud operations using aws dynamo db with flask ap is and boto3Katy Slemon
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web ServicesBradley Holt
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Gheyath M. Othman
 
Introducere in web
Introducere in webIntroducere in web
Introducere in webAlex Eftimie
 
Read, store and create xml and json
Read, store and create xml and jsonRead, store and create xml and json
Read, store and create xml and jsonKim Berg Hansen
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Gheyath M. Othman
 
When RSS Fails: Web Scraping with HTTP
When RSS Fails: Web Scraping with HTTPWhen RSS Fails: Web Scraping with HTTP
When RSS Fails: Web Scraping with HTTPMatthew Turland
 

Mais procurados (20)

Psr-7
Psr-7Psr-7
Psr-7
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
Psr 7 symfony-day
Psr 7 symfony-dayPsr 7 symfony-day
Psr 7 symfony-day
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATIONPHP BASIC PRESENTATION
PHP BASIC PRESENTATION
 
Php basic for vit university
Php basic for vit universityPhp basic for vit university
Php basic for vit university
 
SOAP-based Web Services
SOAP-based Web ServicesSOAP-based Web Services
SOAP-based Web Services
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3
 
Advanced Json
Advanced JsonAdvanced Json
Advanced Json
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
ASP.NET WEB API
ASP.NET WEB APIASP.NET WEB API
ASP.NET WEB API
 
Services web RESTful
Services web RESTfulServices web RESTful
Services web RESTful
 
Common Gateway Interface
Common Gateway InterfaceCommon Gateway Interface
Common Gateway Interface
 
Web Development Course: PHP lecture 4
Web Development Course: PHP  lecture 4Web Development Course: PHP  lecture 4
Web Development Course: PHP lecture 4
 
Crud operations using aws dynamo db with flask ap is and boto3
Crud operations using aws dynamo db with flask ap is and boto3Crud operations using aws dynamo db with flask ap is and boto3
Crud operations using aws dynamo db with flask ap is and boto3
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web Services
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Introducere in web
Introducere in webIntroducere in web
Introducere in web
 
Read, store and create xml and json
Read, store and create xml and jsonRead, store and create xml and json
Read, store and create xml and json
 
Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3Web Development Course: PHP lecture 3
Web Development Course: PHP lecture 3
 
When RSS Fails: Web Scraping with HTTP
When RSS Fails: Web Scraping with HTTPWhen RSS Fails: Web Scraping with HTTP
When RSS Fails: Web Scraping with HTTP
 

Semelhante a PHP And Web Services: Perfect Partners

HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.jssouridatta
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统yiditushe
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHPZoran Jeremic
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebookguoqing75
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckrICh morrow
 
Creating native apps with WordPress
Creating native apps with WordPressCreating native apps with WordPress
Creating native apps with WordPressMarko Heijnen
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsOhad Kravchick
 

Semelhante a PHP And Web Services: Perfect Partners (20)

Php hacku
Php hackuPhp hacku
Php hacku
 
HackU PHP and Node.js
HackU PHP and Node.jsHackU PHP and Node.js
HackU PHP and Node.js
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Fatc
FatcFatc
Fatc
 
Php summary
Php summaryPhp summary
Php summary
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
Facebook的缓存系统
Facebook的缓存系统Facebook的缓存系统
Facebook的缓存系统
 
Consuming RESTful Web services in PHP
Consuming RESTful Web services in PHPConsuming RESTful Web services in PHP
Consuming RESTful Web services in PHP
 
PHP and MySQL.ppt
PHP and MySQL.pptPHP and MySQL.ppt
PHP and MySQL.ppt
 
4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook4069180 Caching Performance Lessons From Facebook
4069180 Caching Performance Lessons From Facebook
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
PHP from soup to nuts Course Deck
PHP from soup to nuts Course DeckPHP from soup to nuts Course Deck
PHP from soup to nuts Course Deck
 
Creating native apps with WordPress
Creating native apps with WordPressCreating native apps with WordPress
Creating native apps with WordPress
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 

Mais de Lorna Mitchell

Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP TutorialLorna Mitchell
 
Best Practice in API Design
Best Practice in API DesignBest Practice in API Design
Best Practice in API DesignLorna Mitchell
 
Git, GitHub and Open Source
Git, GitHub and Open SourceGit, GitHub and Open Source
Git, GitHub and Open SourceLorna Mitchell
 
Business 101 for Developers: Time and Money
Business 101 for Developers: Time and MoneyBusiness 101 for Developers: Time and Money
Business 101 for Developers: Time and MoneyLorna Mitchell
 
Things I wish web graduates knew
Things I wish web graduates knewThings I wish web graduates knew
Things I wish web graduates knewLorna Mitchell
 
Teach a Man To Fish (phpconpl edition)
Teach a Man To Fish (phpconpl edition)Teach a Man To Fish (phpconpl edition)
Teach a Man To Fish (phpconpl edition)Lorna Mitchell
 
Tool Up Your LAMP Stack
Tool Up Your LAMP StackTool Up Your LAMP Stack
Tool Up Your LAMP StackLorna Mitchell
 
Understanding Distributed Source Control
Understanding Distributed Source ControlUnderstanding Distributed Source Control
Understanding Distributed Source ControlLorna Mitchell
 
Best Practice in Web Service Design
Best Practice in Web Service DesignBest Practice in Web Service Design
Best Practice in Web Service DesignLorna Mitchell
 
Coaching Development Teams: Teach A Man To Fish
Coaching Development Teams: Teach A Man To FishCoaching Development Teams: Teach A Man To Fish
Coaching Development Teams: Teach A Man To FishLorna Mitchell
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Implementing OAuth with PHP
Implementing OAuth with PHPImplementing OAuth with PHP
Implementing OAuth with PHPLorna Mitchell
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Could You Telecommute?
Could You Telecommute?Could You Telecommute?
Could You Telecommute?Lorna Mitchell
 

Mais de Lorna Mitchell (20)

OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 
Best Practice in API Design
Best Practice in API DesignBest Practice in API Design
Best Practice in API Design
 
Git, GitHub and Open Source
Git, GitHub and Open SourceGit, GitHub and Open Source
Git, GitHub and Open Source
 
Business 101 for Developers: Time and Money
Business 101 for Developers: Time and MoneyBusiness 101 for Developers: Time and Money
Business 101 for Developers: Time and Money
 
Things I wish web graduates knew
Things I wish web graduates knewThings I wish web graduates knew
Things I wish web graduates knew
 
Teach a Man To Fish (phpconpl edition)
Teach a Man To Fish (phpconpl edition)Teach a Man To Fish (phpconpl edition)
Teach a Man To Fish (phpconpl edition)
 
Join In With Joind.In
Join In With Joind.InJoin In With Joind.In
Join In With Joind.In
 
Tool Up Your LAMP Stack
Tool Up Your LAMP StackTool Up Your LAMP Stack
Tool Up Your LAMP Stack
 
Going Freelance
Going FreelanceGoing Freelance
Going Freelance
 
Understanding Distributed Source Control
Understanding Distributed Source ControlUnderstanding Distributed Source Control
Understanding Distributed Source Control
 
Best Practice in Web Service Design
Best Practice in Web Service DesignBest Practice in Web Service Design
Best Practice in Web Service Design
 
Coaching Development Teams: Teach A Man To Fish
Coaching Development Teams: Teach A Man To FishCoaching Development Teams: Teach A Man To Fish
Coaching Development Teams: Teach A Man To Fish
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Implementing OAuth with PHP
Implementing OAuth with PHPImplementing OAuth with PHP
Implementing OAuth with PHP
 
Web Services Tutorial
Web Services TutorialWeb Services Tutorial
Web Services Tutorial
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Example Presentation
Example PresentationExample Presentation
Example Presentation
 
Could You Telecommute?
Could You Telecommute?Could You Telecommute?
Could You Telecommute?
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Último

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Último (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

PHP And Web Services: Perfect Partners

  • 1. PHP and Web Services: Perfect Partners
  • 2. About Me ● Lorna Jane Mitchell ● PHP Developer/Trainer/Consultant at Ibuildings ● Hosting DPC ● Editor at techPortal ● http://lornajane.net ● @lornajane
  • 3. PHP ● PHP: "solving the web problem" ● The problem evolves ● PHP evolves
  • 4. PHP ● Server ● Client ● Web application tool ● Command line tool ● GTK ...
  • 5. PHP ● Ubiquitous ● Runs on your platform ● *nix ● mac ● windows (yes really, rather well!) ● Can talk to your other systems
  • 6. Web Services ● "scalable, reusable libraries" ● Interface over HTTP ● Modular application design
  • 14. 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
  • 15. Writing JSON from PHP 1 <?php 2 3 $menu['starter'] = array( "prawn cocktail", 4 "soup of the day"); 5 $menu['main course'] = array( "roast chicken", 6 "fish 'n' chips", 7 "macaroni cheese"); 8 $menu['pudding'] = array( "cheesecake", 9 "treacle sponge"); 10 11 echo json_encode($menu); {"starter":["prawn cocktail","soup of the day"],"main  course":["roast chicken","fish 'n' chips","macaroni  cheese"],"pudding":["cheesecake","treacle sponge"]}
  • 16. Reading JSON from PHP 1 <?php 2 3 $json = '{"starter":["prawn cocktail","soup of the day"],"main course":["roast chicken","fish 'n' chips","macaroni cheese"],"pudding":["cheesecake","treacle sponge"]}'; 4 5 print_r(json_decode($json));
  • 17. 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 ) )
  • 18. XML ● eXtensible Markup Language ● Familiar ● Can give more detail than JSON ● Native read/write in most languages
  • 19. Working with XML from PHP ● Lots of options ● SimpleXML ● DOM
  • 20. SimpleXML Example 1 <?php 2 3 $xml = <<< XML 4 <?xml version="1.0" ?> 5 <menus> 6 <menu>Lunch</menu> 7 <menu>Dinner</menu> 8 <menu>Dessert</menu> 9 <menu>Drinks</menu> 10 </menus> 11 XML; 12 13 $simplexml = new SimpleXMLElement($xml); 14 var_dump($simplexml);
  • 21. 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" } }
  • 22. SimpleXML Example 1 <?php 2 3 $simplexml = simplexml_load_string('<?xml version="1.0" ?><menus/>'); 4 $simplexml->addChild('menu','Lunch'); 5 $simplexml->addChild('menu','Dinner'); 6 $simplexml->addChild('menu','Drinks'); 7 $simplexml->addChild('menu','Dessert'); 8 9 echo $simplexml->asXML();
  • 23. SimpleXML Example <?xml version="1.0"?> <menus> <menu>Lunch</menu> <menu>Dinner</menu> <menu>Dessert</menu> <menu>Drinks</menu> </menus>
  • 25. Service Types ● *-RPC – XML-RPC – JSON-RPC ● SOAP ● REST
  • 26. RPC ● All URLs point to a single endpoint ● Parameters give method names ● Request body can take a variety of formats
  • 27. Example RPC services ● Using Flickr's XML-RPC ● Test method: just echoes back to user ● XML formatted data
  • 28. Flickr Echo Example: XML <?xml version="1.0"?> <methodCall> <methodName>flickr.test.echo</methodName> <params> <param> <value> <struct> <member> <name>api_key</name> <value>....</value> </member> </struct> </value> </param> </params> </methodCall>
  • 29. RPC from PHP: curl 1 <?php 2 3 // $xml is existing SimpleXMLElement Object 4 $url = 'http://api.flickr.com/services/xmlrpc/'; 5 $ch = curl_init($url); 6 7 curl_setopt($ch, CURLOPT_POST, 1); 8 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml->asXML()); 9 10 $response = curl_exec($ch); 11 curl_close($ch);
  • 30. RPC from PHP: pecl_http 1 <?php 2 3 $url = 'http://api.flickr.com/services/xmlrpc/'; 4 5 // procedural method 6 $response = http_post_data($url, $xml->asXML()); 7 8 // alternative method 9 $request = new HTTPRequest($url, HTTP_METH_POST); 10 $request->setRawPostData($xml->asXML()); 11 $request->send(); 12 $response = $request->getResponseBody(); 13 14 var_dump($response);
  • 31. Flickr Response <?xml version="1.0" encoding="utf-8" ?> <methodResponse> <params> <param> <value> <string>&lt;api_key&gt;54rt346&lt;/api_key&gt; </string> </value> </param> </params> </methodResponse>
  • 32. RPC Advantages ● RPC is a great format for wrapping existing functionality ● Can abstract between existing systems ● Familiar functional paradigm
  • 33. Delivering RPC ● Consumers will need – Service URL – Docs of functions and arguments – If this was an existing system, existing docs may suffice
  • 34. 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
  • 35. Wrapping RPC Example 1 <?php 2 3 class Handler 4 { 5 function __call($method, $args) { 6 $ch = curl_init('http://localhost'); 7 $data['method'] = $method; 8 foreach($args as $a) $data[$a] = $a; 9 10 curl_setopt($ch, CURLOPT_POST,1); 11 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 12 $response = curl_exec($ch); curl_close($ch); 13 14 var_dump($response); 15 } 16 } 17 $h = new Handler(); 18 $h->dance('cat','dog','rabbit','penguin'); 19
  • 36. SOAP ● Special case of RPC using XML ● Has given formats for messages and errors ● Libraries exist for creating server and client in most languages
  • 37. PHP SOAP Server Example require_once('lib/myClass.php'); $server = new SoapServer("service.wsdl"); $server->setClass("MyClass"); $server->handle();
  • 38. PHP SOAP Client Example $wsdl = "Service.wsdl"; $client = new SoapClient($wsdl, $params); $output = $client->requestShot( 'http://www.php.net','', 300, 400);
  • 39. 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
  • 40. WSDL <?xml version ='1.0' encoding ='UTF-8' ?> <definitions name='MyClass' targetNamespace='urn:MyClassInventory' xmlns:tns='urn:MyClassInventory' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenc=' http://schemas.xmlsoap.org/soap/encoding/' xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' xmlns='http://schemas.xmlsoap.org/wsdl/'> <message name='getAccountStatusRequest'> <part name='accountID' type='xsd:string'/> </message> <message name='getAccountStatusResponse'> <part name='accountID' type='xsd:string'/> <part name='counter' type='xsd:float' /> </message> <portType name='MyClassPortType'> <operation name='getAccountStatus'> <input message='tns:getAccountStatusRequest'/> <output message='tns:getAccountStatusResponse'/> </operation> </portType> <binding name='MyClassBinding' type='tns:MyClassPortType'> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> <operation name='getAccountStatus'> <soap:operation soapAction='urn:xmethods-delayed-quotes#getAccountStatus'/> <input> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </input> <output> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </output> </operation> </binding> <service name='MyClassService'> <port name='MyClassPort' binding='tns:MyClassBinding'> <soap:address location='http://rivendell.local:10002/MyClassServiceServer.php'/> </port> </service> </definitions>
  • 41. Delivering SOAP ● In WSDL mode, only the WSDL needs to be supplied ● Otherwise method names, arguments and types will be needed
  • 42. 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
  • 43. Implementing REST ● Standard application architecture ● Routing to map requests to internal functionality ● Output not always HTML
  • 44. REST CRUD Action HTTP Verb Retrieve GET Create POST Update PUT Delete DELETE
  • 45. REST Examples ● GET ● http://localhost/users ● http://localhost/users/harry ● POST ● http://localhost/users ● PUT ● http://localhost/users/harry
  • 46. REST from PHP: GET 1 <?php 2 3 $result = file_get_contents('http://localhost/users'); 4 var_dump($result);
  • 47. REST from PHP: GET 1 <?php 2 3 $ch = curl_init('http://localhost/users'); 4 5 curl_exec($ch); ● Health Warning! ● curl will echo output ● use CURLOPT_RETURNTRANSFER to capture it instead
  • 48. REST from PHP: POST 1 <?php 2 3 $ch = curl_init('http://localhost/users'); 4 5 $data = array ("name" => "Cho Chang", 6 "house" => "Ravenclaw"); 7 8 curl_setopt($ch, CURLOPT_POST, 1); 9 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 10 11 curl_exec($ch);
  • 49. REST from PHP: PUT 1 <?php 2 3 $ch = curl_init('http://localhost/users/cho'); 4 5 $data = array ("name" => "Cho Chang", 6 "house" => "Ravenclaw" 7 "age" => 15); 8 9 curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT'); 10 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 11 12 curl_exec($ch); 13
  • 50. REST from PHP: DELETE 1 <?php 2 3 $ch = curl_init('http://localhost/users/ginny'); 4 5 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 6 7 curl_exec($ch);
  • 51. Delivering REST ● Full documentation including URL formats, data types, and response formats ● Must include information about error handling
  • 52. 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" :)
  • 53. Resources ● RESTful Web Services – Leonard Richardson and Sam Ruby ● http://php.net ● http://benramsey.com ● http://lornajane.net
  • 55. Thankyou ● Lorna Jane Mitchell ● http://ibuildings.com ● http://lornajane.net ● @lornajane ● http://slideshare.net/lornajane