Web Services with PHP 5

PCQ Bureau
New Update

Before version 5 of PHP, one had to hunt for a Web service implementation for PHP, may it be the one downloaded from Pear (http:// or NuSOAP (http:// 


However version 5, bundles libraries and functions to create and consume SOAP-based Web services with PHP. This article explains how to install PHP 5 on a PCQLinux 2004 machine and walks you through an example of creating and consuming Web services with PHP 5. 

Install PHP 5

Before installing PHP 5, you must have the RPMs httpd-2.0.47-10.i386.rpm and httpd-devel-2.0.47-10.i386.rpm (found on PCQLinux 2004 CDs). Note that the installation of these RPMs may complain about requirement of a couple of packages. You will find the RPMS for the required packages on the PCQLinux CDs. Install the RPMs using the command 'rpm -ivh '. 

Direct Hit!

Web developers

Create a 'database-enabled' Web service in PHP5

We now come to installing PHP 5. You can find the PHP 5 archive on the September 2004 PCQuest Essential CD. Extract the archive named php-5.0.0.tar.bz2 using the following command.

tar -jxvf php-5.0.0.tar.bz2 

This will produce a directory named php-5.0.0. This will produce a directory named php-5.0.0. Change to this directory and issue the following command.


./configure --with-apxs2 --with-mysql --enable-soap


make install

Copy the file named php.ini-dist (found in the php-5.0.0 directory) to the directory /usr/local/lib as php.ini. Fire up a Linux text editor like joe or gedit and key in the following lines.

AddType application/x-httpd-php .php

DirectoryIndex index.php


Save the above lines in a file named php.conf in the directory /etc/httpd/conf.d. Note that if you have already installed PHP 4 that comes with PCQlinux 2004, back up the existing php.conf file before saving the above file. Finally start/restart Apache Web server using the following command.

/etc/rc.d/init.d/httpd restart

To see if the PHP 5 is properly working, drop a file named phpinfo.php with the following content


in the directory /var/www/html. Invoke the script by typing in the URL http://localhost/phpinfo.php in a Linux Web browser (Mozilla or Konqueror). You should see a comprehensive page about the configuration of your PHP installation along with the version (5.0.0) at the top. 


As an example we code a Web service that queries a table named 'info' of a MySQL database, named employee. Given the last name of the employee (which we assume to be unique), the Web service will return his details like first name designation, department and phone number. We assume that you have worked with MySQL database and know how to create a database and execute queries with MySQL. Create a database named employee. Below is the SQL for creating a table named info in the employee database.


CREATE TABLE info (id smallint(6) NOT NULL auto_increment,firstname varchar(20) default NULL,lastname varchar(20) NOT NULL default '',designation varchar(10) default NULL,department varchar(10) default NULL,phone varchar(10) default NULL,PRIMARY KEY (id),UNIQUE KEY lastname


Next, populate the table using Insert queries like below.

insert into info (firstname,lastname,designation,department,phone) values

('sanjay','majumdar','sr reviewer','pcquest','12395');


insert into info (firstname,lastname,designation,department,phone) values


The Web service

Our Web service will have a function called getInfo( ) that will accept the last name as a String parameter and return an array of Strings for the corresponding employee's info. Below is the code for this Web service.

function getInfo($lastname){

$connection = mysql_connect("localhost",""," ");

$db = mysql_select_db("employee",$connection);

$query_result = mysql_query("select * from info where lastname='$lastname'");

$row = mysql_fetch_array($query_result,MYSQL_ASSOC);

return $row;


$server = new SoapServer("employee.wsdl");




Substitute and with a MySQL user and password who can access the employee database. Save the above lines in a file named employee.php in the directory


Talking about the code, the Web service specific lines are only the last three lines. The lines above those are a conventional PHP function. The last three lines create a new SOAP server, publish the function getInfo( ) (defined above) and finally set the SOAP server to handle the requests from the clients. As simple as it looks? Not really. The SoapServer function (actually a constructor) accepts a parameter that is a WSDL (Web Services Description Language) file and we have to write our own WSDL file that is definitely complex. Unlike other languages, such as Java and .NET, PHP does not have an automatic WSDL generation tool wherein a WSDL file gets generated automatically when fed with a PHP script page. We have posted the WSDL file to expose the function getInfo( ) as a Web Service under the Developer Stuff section at 

Save the downloaded file in the directory /var/www/html. In the WSDL file, note the following line: 

This line specifies the URL to the Web Service. 

The consumer

Finally, below is the code for a PHP client which can use our published Web service.

$client = new SoapClient("employee.wsdl");

$arr = $client->getInfo("govindarajan");



print("Employee Not Found");




FirstName: ) ?>

LastName: ) ?>

Designation: ) ?>

Department: ) ?>

Phone: ) ?>

Only the first two lines are specific to consuming a Web service. With the first line a new SOAP client is created with the location of same WSDL file - the one used while coding the Web Service above. And the second line calls the function getInfo( ). Save the above code in a file named client.php in the directory /var/www/html. Access the client using the URL http://localhost/client.php to see the Web Service working.

Shekhar Govindarajan IT4Enterprise