RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


SOAP's Alive: Try the New Native SOAP Extensions for PHP : Page 3

With PHP5, Web services and everybody's favorite open source Web development language are now native pals.

Consuming a Web Service
The nice folks at xmethods.net have provided a lot of Web services to the public, mostly for free, that can be used for building or testing your applications. A great example Web service that you can consume is Xmethods' Temperatures service, which returns the current temperature at a given ZIP code. You'll need a very simple client to consume it:

   $ZIP = $_GET['zipcode'];
   $client = new SoapClient
   $return = $client->getTemp($ZIP);
   echo("Temperature is: " . $return);
Figure 3. The Air in Times Square: You can get the current temperature in any ZIP code using PHP SOAP client.
The simplicity of this code shows you the power of the new SOAP extensions to PHP. In the second line, you create a proxy to the Web service called client, by simply constructing a new client based on the WSDL of the Web service. This is the 'Web Services Description Language' in action: This WSDL is enough to create, on the fly, an object that represents locally all the functions of the distant service, and allows you to invoke them as if the distant service was an inline object. The next line does just that—invoking the 'getTemp' method of the proxy, passing it the ZIP code that was passed in turn to this page using a URL parameter. Therefore, if you use your browser to save this file in the phptest directory from earlier and call the URL http://localhost/phptest/client.php?zipcode=10036, the browser will render the current temperature in Times Square in New York City (see Figure 3).

Now, building a client is all very nice, but what about building your own Web service in PHP? You'll be glad to hear that it is just as easy, as you will see in the next section.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date