Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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


advertisement
Build Your Very Own Web Service with PHP5
In the previous section you built a client that consumed the xmethods Web service that provides temperature information for a specific ZIP code. In this section you will build a simulation of that service, which returns a random number between 40 and 80 instead of the real temperature (interfacing your Web service with the national weather service is a little beyond the scope of this article).

Begin with the following code, a PHP Web service that returns a random number between 40 and 80:

<?php function getTemp($zip) { $temp = rand(40,80); return $temp; } ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache $server = new SoapServer("temperature.wsdl"); $server->addFunction("getTemp"); $server->handle(); ?>

The code is very simple, but if you try to run it, it won't work. The reason it won't work is because it needs to create a new SoapServer based on the WSDL document temp.wsdl, which doesn't yet exist because you haven't created it. Just like the SoapClient creates itself based on the Web services definition language, this creates a server as defined in that language.



If you've ever worked with WSDL, you'll know that it is very tricky to write from scratch. A lot of development environments and IDEs generate WSDL automatically for you from your Web service classes. For example, when using Microsoft Visual Studio.NET to create C# Web services, you have a wizard that creates the Web service for you based on a C# class. As you add Web methods to the class, the WSDL is automatically generated by the runtime environment for you.

Using PHP you don't have this luxury, as you need the WSDL first. Therefore you'll have to start sharpening your WSDL coding skills to be able to use Web Services in PHP. A nice idea for a new open source project would be a WSDL generator that takes a PHP class and generates WSDL from it!

The WSDL for the temperature Web service is shown in Listing 1.

This isn't the most intuitive document in the world, but after looking at a few of them you'll get the hang of what is going on. A great way to start getting used to WSDL is to cut and paste this code, and as you grow your Web service, expand the document as appropriate, describing the new methods that you add.

If you put this WSDL into a text file called temperature.wsdl, and store it in the same directory on your server as the server.php file, you are now ready to run and consume this Web service.

To consume it and prove it works, you will need to amend the Web service client from earlier to point at the new service instead of the xmethods one:

<?php $ZIP = $_GET['zipcode']; $client = new SoapClient("temperature.wsdl"); $return = $client->getTemp($ZIP); echo("Temperature is: " . $return); ?>

Browsing to this page will give much the same results as earlier, except of course this time the temperature isn't real, but instead a random number generated by the Web service.

Going on from Here
Web services are an incredibly useful and efficient technology and hopefully you now have the basics to use them with PHP, while letting PHP's SoapClient and SoapServer classes do most of the grunt work. While the example server itself doesn't do much, just returning a random number, the sky is the limit for expansion.

One of the great features of PHP is its tight integration with the MySQL database. If you need to secure and control access to your data through a standardized interface, it would be very easy to build upon what you learned here. A Web service that fronts a database and exposes its contents via Web methods is all you would need.

In this article you learned the basics of building and consuming Web services using PHP. This opens up new vistas for PHP coders, who have been restricted before with no native support for SOAP. While there were SOAP libraries available in PHP, only now is it a native part of the language itself, instead of a third party add-on.

Using the SoapServer classes you learned how to build a Web service that exposes your data to standard SOAP interfaces and using the SoapClient class you learned how to consume this and other third-party Web services. All the tools are there in your toolbox to build integratable PHP Web sites. These are the kinds of advancements that will allow PHP to evolve beyond its niche as a cheap alternative for homegrown Web site delivery and into a premier and professional Web application development language.



Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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