Jul
14
    
Posted (mistermonty) in PHP on July-14-2007

In this post I’ll show you what a webservice is and how to use it from your PHP website with a very short and simple example. A webservice is a service running on a webserver. This service can be anything from a simple calculation to a full search engine service. You can use a webservice by making a service request(over a network or the Internet) on a client computer. Just like a function in PHP, a webservice request usually requires certain parameters and returns a result to the client.

Each webservice has an XML description which describes its services. This is written in WDSL and stands for Web Service Description Language. Basically it’s just an xml file with all the functionality of the webservice and how to request this functionality.

The interaction between client and webservice happens with the SOAP protocol. SOAP stands for Simple Object Access Protocol and exchanges information over HTTP. Don’t worry, you don’t need to know all the technical details of this protocol. Even though the SOAP extension is not installed by default, PHP makes it really easy to use it. To show you how it works, Ill use the Google Search API as an example. Google provides a WDSL file, so it’s really easy to use their web services.

Step 1: Enable the SOAP extension in PHP.

To do this, open up your php.ini file (usually located in your windows directory) and add the following line at the end of the extensions list:

extension = php_soap.dll

After you added and saved your php.ini, restart your web server or ask your web host to restart it for you.

Step 2: Request a Google API key.

Go to http://code.google.com/ to request your API key if you don’t have one yet. It’s free. They need to identify where the webservice requests are coming from, because on some services the amount of requests are limited per day. You will need this key to pass as a parameter when you do a google webservice request.

Step 3: Create a SOAP client object in your PHP code.

To do this, create an instance of the SoapClient class. You can pass the WDSL of the webservice in the constructor when you initiate your object. This description is located here: http://api.google.com/GoogleSearch.wsdl

<?php
...
$soapclient = new SoapClient("http://api.google.com/GoogleSearch.wsdl");
...
?>

If you look at the WDSL file more closely, you can see that this webservice offers a function to do google searches.

Step 4: Call the Google Search function.

The google search function is called doGoogleSearch and has 10 parameters. An example of this function try the following PHP code:

<?php
...
$searchresults  = $soapclient->doGoogleSearch("yourGoogleAPIkey", "photoshop tutorials site:www.photoshoptalent.com", 1, 10, false, '', false, '', '', '');
...
?>

This request will get you the first 10 results of the query “photoshop tutorials site:www.photoshoptalent.com” and will store the results in $searchresults.

Step 5: output the search results.

This webservice delivers you alot of information, like the total amount of results of your query, the duration of the search, search recommendations, … In this example I’ll show you only the actual search results, which are stored in the resultElements property of our results variable. Let’s loop over the results and output them to the browser:

<?php
...
foreach ($searchresults->resultElement as $key =&gt; $value) { echo " <p><a href="$value-&gt;URL">$value-&gt;URL</a></p>"; }
...
?>

For more information about google’s webservices, please go here: http://code.google.com/apis/soapsearch/api_faq.html
For more useful webservices, check out http://www.xmethods.net/

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!



You must be logged in to post a comment.