Last Updated: 01 Jul 2023
|
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
backend-tech:php:using-soap-rest-web-services-in-php [Apr 18, 2009 12:47 AM] dordal |
backend-tech:php:using-soap-rest-web-services-in-php [Jul 1, 2023 05:35 PM] dordal old revision restored (Nov 21, 2020 10:39 PM) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | = Using SOAP & REST Web Services in PHP = | ||
+ | |||
+ | PHP lets you pretty easily consume data from Web Services. There are two main types: REST and SOAP. This document outlines how to connect to a web service of each type and get some basic data from it. | ||
+ | |||
+ | == Using REST Web Services in PHP == | ||
+ | |||
+ | REST web services are simple and easy to call, but don't provide a lot of structure. Basically, you make a standard HTTP call (either a GET or POST) to a web server, and it returns an XML document with the response (just like loading a web page, except you're getting an XML document back instead of an HTML/XHTML document). You then use PHP's standard XML processing routines (such as SimpleXML) to parse this document and get at the actual data you need. | ||
+ | |||
+ | ' | ||
+ | |||
+ | === Calling a REST Web Service === | ||
+ | |||
+ | Calling a REST web service is easy: just create a query string, have '' | ||
+ | |||
+ | <code php> | ||
+ | // Call the Yahoo BOSS search service | ||
+ | |||
+ | $query = rawurlencode(' | ||
+ | $appId = ' | ||
+ | $url = " | ||
+ | |||
+ | $c = curl_init($url); | ||
+ | curl_setopt($c, | ||
+ | $responseXML = curl_exec($c); | ||
+ | curl_close($c); | ||
+ | |||
+ | $response = simplexml_load_string($responseXML); | ||
+ | |||
+ | // dump out the XML object. Normally, we'd do something useful with it. | ||
+ | var_dump($response); | ||
+ | </ | ||
+ | |||
+ | == Using SOAP Web Services in PHP == | ||
+ | |||
+ | A SOAP web service is a little more complex than a REST web service. SOAP, a.k.a. Simple Object Access Protocol, is a W3C standard that is (theoretically) designed to standardize the exchange of data between machines, even machines running different operating systems, software, etc. The actual description of a SOAP web service interface is specified by a WSDL (a.k.a. Web Service Description Language), which tells the SOAP client what sorts of requests it can make, what format to send data in, what format to expect the returned data in, etc. | ||
+ | |||
+ | Like a REST web service, a SOAP web service operates over standard HTTP and exchanges data via XML. Unlike REST, SOAP sends/ | ||
+ | |||
+ | PHP has a variety of SOAP implementations including [[http:// | ||
+ | |||
+ | === Calling a SOAP Service === | ||
+ | |||
+ | PHP5's built-in SOAP client, [[http:// | ||
+ | |||
+ | <code php> | ||
+ | // Call the Cdyne Weather Service. Debugging mode enabled (e.g. ' | ||
+ | $client = new SOAPClient(' | ||
+ | array(' | ||
+ | |||
+ | try { | ||
+ | $params-> | ||
+ | // parameters, we can add additional elements to | ||
+ | // the array: $params-> | ||
+ | $weather = $client-> | ||
+ | } catch (SOAPFault $exception) { | ||
+ | // if we hit an error, print the exception and the XML we sent | ||
+ | print $exception; | ||
+ | print htmlspecialchars($client-> | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | // normally, we'd do something useful with the result | ||
+ | var_dump($weather); | ||
+ | </ | ||
+ | |||
+ | === SOAP Debugging === | ||
+ | |||
+ | Debugging SOAP calls is fairly annoying. The best way to do it is with the magic functions '' | ||
+ | |||
+ | Once you have the XML, you can fiddle with the SOAP setup so that it constructs an appropriate XML document. | ||
+ | |||
+ | === SOAP Headers - Passing Authentication Information === | ||
+ | |||
+ | Many web services require authentication. If you're using a REST web service, you generally just pass your login, application ID, password or other authentication information as part of the call, e.g.:< | ||
+ | |||
+ | You create the SOAP headers with the '' | ||
+ | === Advanced Example: Google Keyword Tool Service === | ||
+ | |||
+ | Here's a more advanced SOAP example, which connects to the [[http:// | ||
+ | |||
+ | <code php> | ||
+ | // Set config and authentication information | ||
+ | $email = ' | ||
+ | $password = ' | ||
+ | $client_email = ''; | ||
+ | $useragent = ' | ||
+ | $developer_token = ' | ||
+ | $application_token = ''; | ||
+ | |||
+ | $namespace = ' | ||
+ | |||
+ | // setup a SOAP client, and assign the authentication headers to $client | ||
+ | $client = new SOAPClient(' | ||
+ | array(' | ||
+ | $email = new SOAPHeader($namespace, | ||
+ | $password = new SOAPHeader($namespace, | ||
+ | $useragent = new SOAPHeader($namespace, | ||
+ | $token = new SOAPHeader($namespace, | ||
+ | $client_email = new SOAPHeader($namespace, | ||
+ | $headers = array($email, | ||
+ | $client-> | ||
+ | |||
+ | // setup parameters to pass | ||
+ | $params-> | ||
+ | $params-> | ||
+ | $params-> | ||
+ | $params-> | ||
+ | $params-> | ||
+ | $params-> | ||
+ | |||
+ | try { | ||
+ | // make the call to the API | ||
+ | $variations = $client-> | ||
+ | } catch (SOAPFault $exception) { | ||
+ | var_dump($client-> | ||
+ | var_dump($client-> | ||
+ | var_dump($exception); | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | // again, normally we'd do something useful here | ||
+ | var_dump($variations); | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||