Posted by: phillipnb | April 22, 2011

PHP and Web Services – Part 2

In the last edition of this article titled “PHP and Web Services – Part 1”, we had discussed what web service is, the technical terms associated with it etc. In this edition we will work on our first example to demonstrate the use of web services using PHP. In this example, we will create a client using PHP CURL and then post our request to a server (using a predefined url), which will then respond back if our request was valid.

Our first example will be using the web service provided by It is a database of books providing on-line and remote research tools for individuals, book stores, librarians,etc . Registered users can query this database using isbn number, title of the book, author etc. So, how are we going to use the web services provided by Do we need to have some kind of interface to talk to?. Yes, we have an interface and it is called ISBNb’s Application Programming Interface. ISBNdb’s API is designed to allow web clients use the data that is stored by ISBNdb. More details of this API can be got from the url Take a look at their documentation which tells about how to use their service, what services are provided, how to access them etc. Let me demonstrate here about their service.

The way to use ISBNdb’s web service can be summarized into the following steps:

  • In order to use the web service provided by ISBNdb, we need to register. Registration provides each user with an access key.
  • Using this access key and the isbn number (id) of the book to be queried as parameters, we can create a url.
  • We will then use php curl to access the database using this url
  • The response to this curl request will be in the form of an xml

The access protocol specification used here is the web request protocol. Access using other protocols like SOAP/WSDL or XML-RPC is yet to be implemented by isbndb. Let us go ahead and use this web service. To access this web service, let us follow the following steps:

  • Aim of this web service:
    To get the details of the book with id 0596002068 using the access key (we got this after registration) as 1234567A
  • Create the required url:
    Using the access key and book id, we will create the url as,
  • Create the client code:
    The php code to post the above url will be
    $output     = '';
    $curlHandle = '';
    $useragent  = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
    $url        = '';
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_USERAGENT, $useragent);
    curl_setopt($curlHandle, CURLOPT_URL,$url);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER,true);
    $output = curl_exec($curlHandle);
    $info = curl_getinfo($curlHandle);
    if ($output === false || $info['http_code'] != 200) {
    	 echo "\n * No CURL data returned for $url,http code = [". $info['http_code']. "]";
    	 if (curl_error($curlHandle)) {
    	     echo "\n Error: ".curl_error($curlHandle);
    echo $output;
    return $output;

Let us discuss each line of the above code:

  • curl_init
    Initializes the curl session and returns a curl handle
  • curl_setopt
    Tells curl to set an option for this curl handle
  • curlopt_useragent
    Tells the server what client(browser and its make,example:Mozilla 4.0) is used for this request
  • curlopt_url
    Tell curl which url you want to process
  • curlopt_returntranser
    Using this option you can tell curl whether you want the returned content to be displayed by the browser or save it to a variable
  • curl_exec
    Execute this curl session
  • curl_getinfo
    Get information from curl about the recent transfer
  • curl_close
    Close this curl session

When you complete the execution of the above code, the output will be as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2011-04-09T10:51:26Z">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="programming_web_services_with_perl" isbn="0596002068" isbn13="9780596002060">
<Title>Programming Web services with Perl</Title>
<AuthorsText>Randy J. Ray and Pavel Kulchenko</AuthorsText>
<PublisherText publisher_id="oreilly">Farnham ; O'Reilly, 2002 printing, c2003.</PublisherText>

We have the title, author, publisher and other information regarding the book that we queried for, in xml format. Parsing this xml, we can display all the relevant information about this book in html format using a browser.

This brings to the end of our first example. If you find it interesting, please change the code so that it fetches a book based on a different isbn or even a different author.

Have fun and till next time, Happy PHPing


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: