Welcome to TheCredence.com - You may like to subscribe to our RSS feed to stay updated.

This is the second article on semi advanced PHP (as far as i am concerned, I am still learning after all). You can read my previous article on creating RSS Feeds in PHP as well. Anyway this article is about Soap and how can we use it in PHP.

XML web services have become a common option among big websites these days. XML Web Services is a technology that allows for the exposure and consumption of functionality (typically in the form of function calls and replies) over HTTP, independent of platform, language, or network. There are many web services for many purposes ranging from getting weather reports to payment and shipping services. There are many ways to handle web services; but among them SOAP or Simple Object Access Protocol is most popular for advanced features.

What is SOAP ?

SOAP is a simple protocol based on XML that allows applications to send messages back and forth (requests and responses) over HTTP. As mentioned previously, it is both platform and programming language independent.
A complete SOAP message for the preceding fictional web service might then look as follows.

 
<?xml version="1.0"?>
<soap:Envelope
   xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
   soap:encoding="http://www.w3.org/2001/12/soap-encoding">
  <soap:Body>
    <wi:getWeatherForCode
        xmlns:wi="http://somedomain.com/WeatherService">
      <wi:ZipCode>94121</wi:ZipCode>
    </wi:getWeatherForCode>
  </soap:Body>
</soap:Envelope>
 

As you can see it is quite complex. Even more complex are the WSDL files. These files reside in the server and define the services provided by the server. I will not go through all the details of WSDL of SOAP, because we won't need them. Due to the, complexity of using them, there are wrappers available for SOAP in most languages.

Using SOAP in PHP

PHP has a really cool extension for SOAP that makes using SOAP really a piece of SOAP. But Beware, It is PHP 5 only. But after using it you will start embracing its features. First enable the extension in the php.ini. SOAP extension is an object oriented extension. It can be broken into two sections SOAP client and SOAP server. We will discuss SOAP client here.

The class with which we will most often work is the SoapClient class. It is extremely powerful and will hide a majority of the details of working with SOAP and WSDL, further making a compelling case for using XML Web Services.

The easiest way to create an instance of this class is to pass the constructor the URI of the WSDL file for the service, as follows:

 
$client = new SoapClient(wsdl_url);
 

We will use a currency exchange service to in this tutorial. So here is the code to start the service

 
$client = new
SoapClient('http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl');
 

Now calling a service is just like calling a regular PHP function. This is what makes this extension so cool and easy.

 
$exchange = $client->getRate('korea', 'japan');
 

As you can see, it's just a matter of calling a function. But how will you know which services are available?

 
$funcs = $exchange->__getFunctions()

This will return an array of functions available at a SOAP service. There are also many classes and functions that available that let you to get more low level control. You can get full info at official php documentation.

You have learned the basics of web services; but where to use it? There are so many services exposed including The famous Google API, Amazon API and so on. Just about every utility site now holds some kind of web service option. So start getting services and make yourself useful! In the future I will try to cover more on PHP, Soap and services. As usual suggestions are always welcomed to improve the article.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Subscribe in a reader |

Links you may find interesting -