CHAPTER 8 – XML with PHP 5 – SOAP

This section guides you through using SOAP as a client for the Google Web API and implementing your own SOAP server. Because SOAP is even more complex than XML-RPC, we unfortunately can't include everything.

PEAR::SOAP Google is a nice, fast search engine. Wouldn't it be great to have your own command-line search engine written in PHP? This section tells you how. Google To make use of the SOAP API that Google exports, you need an account, which you can create on http://www.google.com/apis/. When you regis- ter, you receive a key via email that you use when you call the SOAP method. For the following example to work correctly, you need to install the PEAR SOAP class, with pear install SOAP. After SOAP is installed, we can start with the following simple script. First, include the PEAR::SOAP class: #!/usr/local/bin/php <?php /* Include the class */ require_once 'SOAP/Client.php'; Next, we define the URL to the SOAP server and instantiate a SOAP_Client object, which we will use to execute our search: /* Create the client object */ $endpoint = 'http://api.google.com/search/beta2'; $client = new SOAP_Client($endpoint); The search string is passed on the command line. If no parameter was passed, we'll display a little usage message: /* Read the search string from the command line */ if ($argc != 2) { echo "usage: ./google.php searchstringnn"; exit(); } $query = $argv[1]; Then, we set up the other parameters for the SOAP call. Note that we don't do anything to specify the type of the variables; we just let the class decide this for us: /* Defining the 'license' key */ $key = 'jx+PnvxQFHIrV1A2rnckQn8t91Pp/6Zg'; /* Defining maximum number of results and starting index */ $maxResults = 3; $start = 0; /* Setup the other parameters */ $filter = FALSE; $restrict = ''; $safeSearch = FALSE; $lr = ''; $ie = ''; $oe = ''; Next, we make the call to Google. The call() method of the SOAP_Client object expects three parameters: The name of the function to call An array with parameters for the call The namespace for the call /* Make the call */ $params = array( 'key' => $key, 'q' => $query, 'start' => $start, 'maxResults' => $maxResults, 'filter' => $filter, 'restrict' => $restrict, 'safeSearch' => $safeSearch, 'lr' => $lr, 'ie' => $ie, 'oe' => $oe ); $response = $client->call( 'doGoogleSearch', $params, array('namespace' => 'urn:GoogleSearch') ); In this example, we assume that the search call returned something use- ful, although it might not always do so. The Google API returns the text with XML entities escaped and with some inserted <br> tags. We convert the enti- ties to normal characters using html_entity_decode() and strip all tags with strip_tags(): /* Display results */ foreach ($response->resultElements as $result) { echo html_entity_decode( strip_tags("{$result->title}n({$result->URL})nn") ) ; echo wordwrap(html_entity_decode(strip_tags($result ->snippet))); echo "nn----------------------------nn"; } ?> Now, let's go to the next example where we implement a simple SOAP cli- ent and server using the same functions as in the XML-RPC examples. SOAP Server Here is the server. First, we include the SOAP_Server PEAR Class. Next, we define a class (Example) with the two functions that we want to export through SOAP. In the hello() method, we use implicit conversion from PHP types to SOAP types; in the add() method, we explicitly define the SOAP type (float): <?php require_once 'SOAP/Server.php'; class Example { function hello ($arg) { return "Hi {$arg}!"; } function add ($a, $b) { return new SOAP_Value('ret', 'float', $a + $b); } } To fire up the server and process the request data that is stored in HTTP_RAW_POST_DATA, we instantiate the SOAP_Server class, instantiate the class with our methods, associate the class with the SOAP_Server, and process the request by calling the service() method of the SOAP_Server object. The service method processes the data that was posted to the PHP script, extracts the function name and parameters out of the XML, and calls the function in our Example class: $server = new SOAP_Server; $soapclass = new Example(); $server->addObjectMap($soapclass, 'urn:Example'); $server->service($HTTP_RAW_POST_DATA); ?> SOAP Client The client is much like the Google client, except that we used explicit typing for the parameters in the call to the add() method: #!/usr/local/bin/php <?php /* Include the class */ require_once 'SOAP/Client.php'; /* Create the client object */ $endpoint = 'http://kossu/soap/server.php'; $client = new SOAP_Client($endpoint); /* Make the call */ $response = $client->call( 'hello', array('arg' => 'Derick'), array('namespace' => 'urn:Example') ); var_dump($response); /* Make the call */ $a = new SOAP_Value('a', 'int', 212.3); $b = new SOAP_Value('b', 'int', 312.3); $response = $client->call( 'add', array($a, $b), array('namespace' => 'urn:Example') ); var_dump($response); ?> This is going over the wire (for the second call). You can see that there is much more XML magic than with XML-RPC: POST /chap_xml/soap/server.php HTTP/1.0 User-Agent: PEAR-SOAP 0.7.1 Host: kossu Content-Type: text/xml; charset=UTF-8 Content-Length: 528 SOAPAction: "" <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns4="urn:Example" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <ns4:add> <a xsi:type="xsd:int">212.3</a> <b xsi:type="xsd:int">312.3</b></ns4:add> </SOAP-ENV:Body> </SOAP-ENV:Envelope> HTTP/1.1 200 OK Date: Tue, 31 Dec 2002 14:56:17 GMT Server: Apache/1.3.27 (Unix) PHP/4.4.0-dev X-Powered-By: PHP/4.4.0-dev Content-Length: 515 Connection: close Content-Type: text/xml; charset=UTF-8 <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns4="urn:Example" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <ns4:addResponse> <ret xsi:type="xsd:float">524</ret></ns4:addResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Post Comment
Login to post comments