NuSOAP


For all SOAP's popularity, the last example probably succeeded in making some aspects of its use rather complicated. Fortunately, this isn't the case. Several existing SOAP classes are available that will save you dealing with all the details. Notable PHP classes include NuSOAP, PEAR:SOAP, PHP SOAP, exSOAP, and Krysalis. I have selected NuSOAP for these examples for a few reasons: namely, ease of install for non-root users, decent compatibility with tested servers, ease of use, and finally, compatibility with both PHP4 and 5.

Here is a brief example using NuSOAP that performs roughly the same action as the previous example:

 <?php require('../lib/nusoap.php'); $client = new soapclient("http://example.org/googleapi/GoogleSearch.wsdl", true); 

In this example, you require the NuSOAP library, and create the client object. When creating the soapclient object, you can do it one of two ways: You can specify the location of the wsdl file that describes the service, or you can specify the target for the URL for the service. If you use the first method (as shown in the preceding code), the second parameter must be set to true; if you use the second method, it must be false.

 $client->soap_defencoding = 'UTF-8'; $query = array(   'key'=>'u6U/r39QFHK18Qcjz/XdWSbptVaj9k1t',   'q'=>'paul reinheimer',   'start'=>0,   'maxResults'=>2,   'filter'=>true,   'restrict'=>'',   'safeSearch'=>true,   'lr'=>'',   'ie'=>'',   'oe'=>'' ); 

Here you see the default encoding set to UTF-8 (remember that Google only accepts UTF-8 in its API, and will only send UTF-8 as a response), and generation of the query object. The names of the various elements should be familiar from the previous example.

 $result = $client->call("doGoogleSearch", $query, "urn:GoogleSearch",   "urn:GoogleSearch"); echo '<pre>'; print_r($result); echo '</pre>'; ?> 

Finally, the $result object is created to contain the results of the query itself, and the results are printed to the screen.

 Array (   [directoryCategories] =>   [documentFiltering] => 1   [endIndex] => 2   [estimateIsExact] =>   [estimatedTotalResultsCount] => 2110   [resultElements] => Array   (     [0] => Array     (       [URL] => http://www.preinheimer.com/       [cachedSize] => 60k       [directoryCategory] => Array           (               [fullViewableName] =>               [specialEncoding] =>           )       [directoryTitle] =>       [hostName] =>       [relatedInformationPresent] => 1       [snippet] => <b>...</b> Posted by <b>Paul</b> <b>Reinheimer</b> in Computing at 20:57 | Comments (0) | Trackbacks (0). <b>...</b><br> thanks <b>paul</b>. Posted by <b>Paul</b> <b>Reinheimer</b> at 22:45 | Comments (2) | Trackbacks (0). <b>...</b>       [summary] =>       [title] => preinheimer.com     )     [1] => Array     (       [URL] => http://p2p.wrox.com/blogs_bio.asp?AUTHOR_ID=22424       [cachedSize] => 17k       [directoryCategory] => Array           (               [fullViewableName] =>               [specialEncoding] =>           )       [directoryTitle] =>       [hostName] =>       [relatedInformationPresent] => 1       [snippet] => <b>...</b> p2p.wrox.com Forums, Blogs &gt; <b>Paul</b> <b>Reinheimer&#39;s</b> Bio. Wrox Blogs. Archive RSS<br> Feed, <b>Paul</b> <b>Reinheimer</b>. Homepage: http://www.preinheimer.com. <b>...</b>       [summary] =>       [title] => p2p.wrox.com Forums     )   )   [searchComments] =>   [searchQuery] => paul reinheimer   [searchTime] => 0.035023   [searchTips] =>   [startIndex] => 1 ) 

Visually, the output of this script is quite similar to the outputs of SimpleXML objects that were examined in Chapter 3. Accessing the information provided is just as easy. For example, replacing the output portion of the last script with the following:

 echo "<b>Search Query</b>: <i>" . $result['searchQuery'] . "</i><br>"; $x = $result['startIndex']; $y = $result['endIndex']; if ($result['estimateIsExact']) {   echo "Displaying results $x to $y, out of ". $result['estimatedTotalResultsCount'] . "results<br>"; }else {   echo "Displaying results $x to $y, out of an estimated ". $result['estimatedTotalResultsCount'] . "results<br>"; } $queryResults = $result['resultElements']; foreach($queryResults as $item) {   echo "<a href=\"{$item['URL']}\">{$item['title']}</a><br>";   echo $item['snippet'] . "<br><br>"; } 

gives a more user-friendly output, as shown in Figure 6-1.

image from book
Figure 6-1




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net