25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

XML and Web Services ” 207<br />

ment that describes the function calls made available by a Web service, as well as<br />

any specialized data types needed by it.<br />

Accessing SOAP-based Web Services<br />

The SoapClient class provides what is essentially a one-stop solution to creating a<br />

SOAP client—all you really need to do is provide it with the path to a WSDL file, and<br />

it will automatically build a <strong>PHP</strong>-friendly interface that you can call directly from<br />

your scripts.<br />

As an example, consider the following SOAP request made to the Google Web<br />

Search service:<br />

try<br />

{<br />

$client = new SoapClient(’http://api.google.com/GoogleSearch.wsdl’);<br />

$results = $client->doGoogleSearch($key, $query, 0, 10, FALSE, ’’,<br />

FALSE, ’’, ’’, ’’);<br />

foreach ($results->resultElements as $result)<br />

{<br />

echo ’’;<br />

echo htmlentities($result->title, ENT_COMPAT, ’UTF-8’);<br />

echo ’’;<br />

}<br />

}<br />

catch (SoapFault $e)<br />

{<br />

echo $e->getMessage();<br />

}<br />

This creates a new SOAP client using the the WSDL file provided by Google.<br />

SoapClient uses the WSDL file to construct an object mapped to the methods defined<br />

by the web service; thus, $client will now provide the methods doGetCachedPage(),<br />

doSpellingSuggestion(), and doGoogleSearch(). In our example, the script invokes<br />

the doGoogleSearch() method to return a list of search results. If SoapClient encounters<br />

any problems, it will throw an exception, which we can trap as explained in the<br />

Object-oriented Programming in <strong>PHP</strong> chapter).<br />

The constructor of the SOAPClient class also accepts, as an optional second parameter,<br />

an array of options that can alter its behaviour; for example, you can change the<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!