Web Services Tutorial



Similar documents
XML Processing and Web Services. Chapter 17

Cloud Elements! Marketing Hub Provisioning and Usage Guide!

Fairsail REST API: Guide for Developers

Getting Started with the icontact API

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

OAuth 2.0 Developers Guide. Ping Identity, Inc th Street, Suite 100, Denver, CO

Designing RESTful Web Applications

COMP 112 Assignment 1: HTTP Servers

Beyond The Web Drupal Meets The Desktop (And Mobile) Justin Miller Code Sorcery Workshop, LLC

REST web services. Representational State Transfer Author: Nemanja Kojic

02267: Software Development of Web Services

JobScheduler Web Services Executing JobScheduler commands

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

02267: Software Development of Web Services

Using the Push Notifications Extension Part 1: Certificates and Setup

Lecture 11 Web Application Security (part 1)

Session 6 Patterns and best practices in SOA/REST

Chapter 22 How to send and access other web sites

Outline Definition of Webserver HTTP Static is no fun Software SSL. Webserver. in a nutshell. Sebastian Hollizeck. June, the 4 th 2013

Modern Web Development From Angle Brackets to Web Sockets

Computer Networks. Lecture 7: Application layer: FTP and HTTP. Marcin Bieńkowski. Institute of Computer Science University of Wrocław

Example for Using the PrestaShop Web Service : CRUD

Getting Started Guide for Developing tibbr Apps

Network Technologies

Types of Cloud Computing

T320 E-business technologies: foundations and practice

Integration of Hotel Property Management Systems (HPMS) with Global Internet Reservation Systems

IBM Watson Ecosystem. Getting Started Guide

Cyber Security Challenge Australia 2014

1 Introduction: Network Applications

HTTP. Internet Engineering. Fall Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Distribution and Integration Technologies

Security Testing For RESTful Applications

SIM Configuration Guide. February 2015 Version 1 Document Reference: 8127

WIRIS quizzes web services Getting started with PHP and Java

Contents. 2 Alfresco API Version 1.0

Java Access to Oracle CRM On Demand. By: Joerg Wallmueller Melbourne, Australia

WEB SERVICES TEST AUTOMATION

UFTP AUTHENTICATION SERVICE

Progress OpenEdge REST

Drupal CMS for marketing sites

Protocolo HTTP. Web and HTTP. HTTP overview. HTTP overview

Programming Autodesk PLM 360 Using REST. Doug Redmond Software Engineer, Autodesk

Riverbed Cascade Shark Common REST API v1.0

URLs and HTTP. ICW Lecture 10 Tom Chothia

Crawl Proxy Installation and Configuration Guide

Easy CramBible Lab DEMO ONLY VERSION Test284,IBM WbS.DataPower SOA Appliances, Firmware V3.6.0

JASPERREPORTS SERVER WEB SERVICES GUIDE

A RESTful Web Service Interface to the ATLAS COOL Database

API documentation - 1 -

About This Document 3. Integration and Automation Capabilities 4. Command-Line Interface (CLI) 8. API RPC Protocol 9.

Copyright Pivotal Software Inc, of 10

The PHP 5.4 Features You Will Actually Use

Commerce Services Documentation

WWW. World Wide Web Aka The Internet. dr. C. P. J. Koymans. Informatics Institute Universiteit van Amsterdam. November 30, 2007

THE CHALLENGE OF ADMINISTERING WEBSITES OR APPLICATIONS THAT REQUIRE 24/7 ACCESSIBILITY

Cloud Elements ecommerce Hub Provisioning Guide API Version 2.0 BETA

User and Programmer Guide for the FI- STAR Monitoring Service SE

Magento Search Extension TECHNICAL DOCUMENTATION

7 Why Use Perl for CGI?

A RESTful Web Service for Whois. Andy Newton Chief Engineer, ARIN

CONTRACT MODEL IPONZ DESIGN SERVICE VERSION 2. Author: Foster Moore Date: 20 September 2011 Document Version: 1.7

vshield API Programming Guide vshield Manager 4.1 vshield App 1.0 vshield Edge 1.0 vshield Endpoint 1.0

Administering Jive for Outlook

Network setup and troubleshooting

Getting started with OWASP WebGoat 4.0 and SOAPUI.

APACHE WEB SERVER. Andri Mirzal, PhD N

Testing the API behind a mobile app. Tutorial Marc van t Veer

Force.com REST API Developer's Guide

Implementing HTTPS in CONTENTdm 6 September 5, 2012

Acrobat Connect. Using Connect Enterprise Web Services

Domain Name System (DNS)

Okta/Dropbox Active Directory Integration Guide

Setup Guide Access Manager 3.2 SP3

Cite My Data M2M Service Technical Description

Design REST Services with CXF JAX- RS implementation: best practices and lessons learned

SOA, case Google. Faculty of technology management Information Technology Service Oriented Communications CT30A8901.

Table of Contents. Open-Xchange Authentication & Session Handling. 1.Introduction...3

A Tour of Silex and Symfony Components. Robert

Integrating Complementary Tools with PopMedNet TM

How To Test Your Web Site On Wapt On A Pc Or Mac Or Mac (Or Mac) On A Mac Or Ipad Or Ipa (Or Ipa) On Pc Or Ipam (Or Pc Or Pc) On An Ip

1. When will an IP process drop a datagram? 2. When will an IP process fragment a datagram? 3. When will a TCP process drop a segment?

vcloud Air Platform Programmer's Guide

Hack Yourself First. Troy troyhunt.com

A Java proxy for MS SQL Server Reporting Services

IBM WebSphere Application Server

Login with Amazon. Getting Started Guide for Websites. Version 1.0

Repeater. BrowserStack Local. browserstack.com 1. BrowserStack Local makes a REST call using the user s access key to browserstack.

THE PROXY SERVER 1 1 PURPOSE 3 2 USAGE EXAMPLES 4 3 STARTING THE PROXY SERVER 5 4 READING THE LOG 6

The Hyper-Text Transfer Protocol (HTTP)

REST API Development. B. Mason Netapp E-Series

A detailed walk through a CAS authentication

Cloud Elements! Events Management BETA! API Version 2.0

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

IRF2000 IWL3000 SRC1000 Application Note - Apps with OSGi - Condition Monitoring with WWH push

The Web: some jargon. User agent for Web is called a browser: Web page: Most Web pages consist of: Server for Web is called Web server:

Application layer Web 2.0

Transcription:

Web Services Tutorial

Web Services Tutorial http://bit.ly/oaxvdy (while you re waiting, download the files!) 2

About Me Lorna Jane Mitchell PHP Consultant/Developer Author API Specialist Project Lead on http://joind.in Twitter: @lornajane Website: http://lornajane.net 3

The Plan Talk about web services in general Build a web service Consume a web service Talk about how to make excellent web services 4

The Plan Talk about web services in general Build a web service Consume a web service Talk about how to make excellent web services (we ll hide some actual textbook theory in there somewhere) Ask questions! 4

Theory and Introduction

What Is A Web Service? Means of exposing functionality or data A lot like a web page, but for machines Integration between applications Separation within an application 6

Integration Between Applications Common to use an API internally as well as exposing it Website 3rd Party / Apps API Data Store 7

Separation Within an Application Routing Auth Images Products Orders 8

Separation Within an Application Routing Auth Images Products Orders 9

Web Services and You This is not rocket science You already know most of what you need! 10

Let s Begin

Building Blocks You can make an API from any tools you like Existing MVC setup Simple PHP (as in these examples) Framework modules Component library 12

My First Web Service Make a virtual host e.g. http://api.local Don t forget to restart apache Add an entry to your hosts file <VirtualHost *:80> ServerName api.local ServerAdmin admin@localhost DocumentRoot /var/www/myapi/public <Directory /var/www/myapi/public> AllowOverride All Order deny,allow Allow from All </Directory> </VirtualHost> api.vhost 13

My First Web Service Create the index.php file e.g. /var/www/myapi/public/index.php $data = array( 'format' => 'json', 'status' => 'live' ); echo json_encode($data); public/index.php 14

Consume Your Service Use curl from the command line curl http://api.local For more information about curl: http://curl.haxx.se/ - curl homepage http://bit.ly/esqbmz - my cheat sheet 15

JSON JavaScript Object Notation Originally for JavaScript Native read/write in most languages Simple, lightweight format - useful for mobile In PHP we have json_encode() and json_decode() These work with arrays and objects Our service returns: {'format':'json','status':'live'} 16

Heartbeat Method A method which does nothing No authentication Requires correct request format Gives basic feedback Shows that service is alive 17

Delivering A Web Service Service Documentation Examples A help point 18

HTTP and Data Formats

HTTP HTTP is Hypertext Transfer Protocol. It is designed to exchange information about a request/response. Status Codes (e.g. 200, 404) Headers (e.g. Content-Type, Authorization) Verbs (e.g GET, POST) 20

Status Codes Win or fail? Some common codes: 200 OK 204 No Content 302 Found 301 Moved Permanently 302 Found 400 Bad Request 401 Not Authorised 403 Forbidden 404 Not Found 500 Internal Server Error For more, see: http://bitly.com/h03dxe 21

Working with Status Codes in PHP We can observe status codes with curl, passing the -I switch curl -I http://api.local Let s amend our web service, to return a 302 header header("302 Found", true, 302); $data = array( 'format' => 'json', 'status' => 'live' ); echo json_encode($data); 22

HTTP Verbs More than GET and POST PUT and DELETE to update and delete in a RESTful service HEAD, OPTIONS and others also specified In REST, we use: GET POST PUT DELETE Read Create Update Delete 23

HTTP Headers Headers are the metadata about the content we send/receive Useful headers: Accept and Content-Type: used for content format negotiation User-Agent: to identify what made the request Set-Cookie and Cookie: working with cookie data Authorization: controlling access 24

Accept Header What type of content can the consumer understand? -v with curl to see request and response headers -H to add headers curl -v -H "Accept: text/html" http://api.local Gives the output: * About to connect() to api.local port 80 (#0) * Trying 127.0.0.1... connected * Connected to api.local (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0 > Host: api.local > Accept: text/html > 25

Using the Accept Header We can work out what format the user wanted to see from the Accept header. $data = array( 'status' => 'live', 'now' => time() ); if(false!== strpos($_server['http_accept'], 'text/html')) { echo "<pre>"; print_r($data); echo "</pre>"; } else { // return json echo json_encode($data); } public/headers.php 26

How to REALLY Handle Accept Headers Example accept header (from my browser) text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1 See a much nicer example of this in headers-accept.php Taken almost entirely from the source of arbitracker http://arbitracker.org src/classes/request/parser.php Try this from curl, setting your own headers, and from your browser 27

Content-Type Header The Content-Type header: literally labels the contents of the response. We can include these in our examples: $data = array( 'status' => 'live', 'now' => time() ); if(false!== strpos($_server['http_accept'], 'text/html')) { header('content-type: text/html'); echo "<pre>"; print_r($data); echo "</pre>"; } else { // return json header('content-type: application/json'); echo json_encode($data); } public/headers.php 28

Handling XML Formats We can work with XML in PHP almost as easily Content type is text/xml or application/xml Two XML libraries in PHP SimpleXML bit.ly/g1xpap DOM bit.ly/e0xmzd Give consumers a choice of formats: HTML, XML, JSON... 29

Adding XML to Our Service $data = array( 'status' => 'live', 'now' => time() ); $simplexml = simplexml_load_string('<?xml version="1.0"?><data />'); foreach($data as $key => $value) { $simplexml->addchild($key, $value); } header('content-type: text/xml'); echo $simplexml->asxml(); public/headers.php The result is this: <?xml version="1.0"?> <data><status>live</status><now>1302981884</now></data> 30

Versioning Always include a version parameter or media type http://example.com/api/v4/status http://example.com/api/status Content-Type: application/vnd.myapi.2+json 31

Data Formats Handle multiple formats, by header or parameter JSON XML? Common to detect header, allow parameter override http://example.com/api/v4/status Accept: application/json http://example.com/api/v4/status?format=json Accept: text/html 32

Statelessness Request alone contains all information needed No data persistence between requests Resource does not need to be in known state Same operation performs same outcome 33

Consuming Services from PHP

Consuming Your First Web Service Three ways to consume web services: Via streams (e.g. file_get_contents()) Using the curl extension http://lrnja.net/i5jd9r Using Pecl_HTTP http://lrnja.net/i23yzj 35

Using File_Get_Contents This is the simplest, especially for a GET request $response = file_get_contents('http://api.local/'); var_dump($response); You can set more information in the stream context bit.ly/gxbagv 36

Using Curl This extension is commonly available $ch = curl_init('http://api.local/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); var_dump($response); Look out for the CURLOPT_RETURNTRANSFER; without it, this will echo the output 37

Using Pecl_HTTP This is the most powerful and flexible, and can easily be installed from http://pecl.php.net $request = new HTTPRequest('http://api.local/', HTTP_METH_GET); $request->send(); $response = $request->getresponsebody(); var_dump($response); Strongly recommend pecl_http if you are able to install pecl modules on your platform 38

Service Types

Web Service Types There are a few types of web service RESTful RPC (Remote Procedure Call) XML-RPC JSON-RPC Soap 40

RPC Services These services typically have: A single endpoint Method names Method parameters A return value Often all POST requests A familiar model for us as developers 41

Soap Not an acronym (used to stand for Simple Object Access Protocol) Special case of XML-RPC VERY easy to do in PHP Can be used with a WSDL Web Service Description Language 42

Soap Example: Library Class public function eightball() { $options = array( "Without a doubt", "As I see it, yes", "Most likely", "Reply hazy, try again", "Better not tell you now", "Concentrate and ask again", "Don't count on it", "Very doubtful"); } return $options[array_rand($options)]; /public/soap/library.php 43

Soap Server (don t blink or you will miss it!) include('library.php'); $options = array('uri' => 'http://api.local/soap'); $server = new SoapServer(NULL, $options); $server->setclass('library'); $server->handle(); /public/soap/index.php 44

Consuming a Soap Service To call PHP directly, we would do: include('library.php'); $lib = new Library(); $response = $lib->eightball(); echo $response; Over Soap: $options = array('uri' => 'http://myapi.local', 'location' => 'http://myapi.local/soap/'); try{ $client = new SoapClient(NULL, $options); $response = $client->eightball(); echo $response; } catch (SoapFault $e) { echo "ERROR: ". $e->getmessage(); } 45

Pros and Cons of Soap Many languages have libraries for it.net and Java programmers in particular like it 46

Pros and Cons of Soap Many languages have libraries for it.net and Java programmers in particular like it Weird things happen between languages regarding data types When it works, it s marvellous When it doesn t work, it s horrid to debug (so I ll show you how) 46

Pros and Cons of Soap Many languages have libraries for it.net and Java programmers in particular like it Weird things happen between languages regarding data types When it works, it s marvellous When it doesn t work, it s horrid to debug (so I ll show you how) WSDL is complicated! 46

Working with WSDLs WSDL stands for Web Service Description Language WSDLs were not designed for humans to read They are written in XML, and are very verbose If you do need to read one, start at the END and read upwards in sections Soap uses very strict data typing, which is an unknown concept in PHP Read about WSDLs: bit.ly/enzomp 47

Debugging Soap The Soap extension has some debug methods: Set the options to include trace=1 in the SoapClient getlastrequest(), getlastrequestheaders(), getlastresponse(), getlastresponseheaders() are then available For all web service types, you can use: I like Wireshark (http://www.wireshark.org) Others use Charles (http://www.charlesproxy.com) If you like reading XML then use curl SoapUI (http://soapui.org) 48

Extending Our Service

Consistency Important to retain Naming conventions Parameter validation rules Parameter order Just as you would in library code 50

The Action Parameter As a simple place to start, let s add an action parameter. // route the request (filter input!) $action = $_GET['action']; $library = new Actions(); $data = $library->$action(); /rpc/index.php Here s the code for the Actions class class Actions { public function getsitestatus() { return array('status' => 'healthy', 'time' => date('d-m-y')); } } /rpc/actions.php So try http://api.local/rpc/index.php?action=getsitestatus 51

Small APIs Beware adding new functionality Simple is maintainable Easy to use and understand 52

Adding an Action To add a new action, create a new method for the Actions class, returning an array to pass to the output handlers public function addtwonumbers($params) { return array('result' => ($params['a'] + $params['b'])); } /rpc/actions.php But how do we pass the parameters in? Something like this // route the request (filter input!) $action = $_GET['action']; $library = new Actions(); // OBVIOUSLY you would filter input at this point $data = $library->$action($_get); /rpc/index.php 53

Access Control A few common ways to identify users Username and password with every request Login action and give a token to use OAuth For internal applications, firewalls 54

RPC Service Example: Flickr Take a look at http://www.flickr.com/services/api/ Supports multiple formats (for request and response) Is well-documented and consistent Has libraries helping users to consume it Offers both RPC and RESTful (kind of!) interfaces Follows true XML-RPC (see http://en.wikipedia.org/wiki/xml-rpc) Vast numbers of applications using this API to provide flickr functionality elsewhere 55

RPC vs REST We ve seen an RPC service, but what about REST? The two are quite different RPC services describe protocols, e.g. XML-RPC RPC is a familiar: functions and arguments REST is a set of principles Takes advantage of HTTP features Typically has "pretty URLs", e.g. Twitter is mostly RESTful 56

RESTful Web Service

REST REpresentational State Transfer URLs are unique resource identifiers HTTP verbs indicate which operation should happen We have full CRUD operations on a series of resources Here s one I prepared earlier: /public/rest/ 58

REST as a Religion Beware publishing a service labelled "RESTful" Someone will always tell you it isn t They are probably right The strict rules around REST sometimes fit badly around business requirements Flamewars will ensue Instead: "An HTTP Web Service" 59

Making Our Service Restful We re aiming for URLs that look something like this: http://api.local/rest/user http://api.local/rest/user/3 http://api.local/rest/user/3/friends A specific item is a resource; where you request a list, e.g. /user, this is called a collection 60

All Requests to index.php We want to push all requests through index.php, there is an.htaccess file that does this <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME}!-f RewriteCond %{REQUEST_FILENAME}!-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> /public/rest/.htaccess 61

Routing Within Our App So our routing depends on the URL and on the verb used, so our controllers have a method per verb: GET /user Becomes UserController::GETAction() // route the request (filter input!) $verb = $_SERVER['REQUEST_METHOD']; $action_name = strtoupper($verb). 'Action'; $url_params = explode('/',$_server['path_info']); $controller_name = ucfirst($url_params[1]). 'Controller'; $controller = new $controller_name(); $data = $controller->$action_name($url_params); // output appropriately $view->render($data); /public/rest/index.php 62

A Simple Controller Now in UserController we add a GETAction class UsersController { public function GETAction($parameters) { $users = array(); // imagine retreving data from models } } if(isset($parameters[2])) { return $users[(int)$parameters[2]]; } else { return $users; } /public/rest/controllers.php 63

Extending our Service Next we will add something to get the user s friends http://api.local/users/1/friends class UsersController { public function GETAction($parameters) { $users = array(); $friends = array(); // imagine retreving data from models } } if(isset($parameters[2])) { if(isset($parameters[3]) && $parameters[3] == 'friends') { return $friends[(int)$parameters[2]]; } else { return $users[(int)$parameters[2]]; } } else { return $users; } /public/rest/controllers.php 64

Hypermedia The items returned are resources with their URLs This is hypermedia - providing links to related items/collections Allows a service to be self-documenting Allows a service to change its URLs - because they re provided 65

Creating New Records RESTful services use POST for creating data, so we have POSTAction curl -X POST http://api.local/users -d name="fred Weasley" public function POSTAction() { // sanitise and validate data please! $data = $_POST; // create a user from params $user['name'] = $data['name']; // save the user, return the new id } // redirect user header('201 Created', true, 201); header('location: http://api.local/rest/users/5'); return $user; /public/rest/controllers.php 66

Updating Records To create: we used POST To update: we use PUT The code looks basically the same, apart from: // instead of this: $data = $_POST; // use this: parse_str(file_get_contents('php://input'), $data); 67

Appropriate Status Codes for REST GET 200 or maybe 302 if we found it somewhere else 404 if we can t find it POST 201 and a location header to the new resource or 400 if we can t create an item PUT 204 for OK (but no content) 400 if the supplied data was invalid DELETE 200 at all times, for idempotency 68

Responding to Failure Use expected response format Collate all errors and return Help users help themselves Be consistent Be accurate 69

Delivering Services

Technical Aspects All PHP Best Practices apply equally to APIs Source control Unit/Integration testing Automated deployment Monitoring Reliability and consistency are key 71

User Aspects Who will use your API? Offer API Docs Write a quick start for the impatient Show examples, as many as possible Tutorials, blog posts, links to forum answers, anything Respond to questions 72

Questions?

Resources All links are in the bit.ly bundle bit.ly/emrpyt 74

Thanks https://joind.in/6217 @lornajane http://lornajane.net 75