How to Publish Web Services Using PHP

PCQ Bureau
New Update

Core PHP, when downloaded from and put to use, can be compared to a

simple Web scripting language such as ASP. But when PHP is used with frameworks

such as Prado (, Symfony ( and CakePHP

( it can be compared to ASP.Net and Java Server Faces. These

frameworks impart component and event based features to core PHP and much more —

they facilitate authentication, AJAX, caching and Web services.

Direct Hit!
Applies To:


A simple (using a library) and

scalable (through a web service) way to retrieve Gmail contacts

Primary Link:

php gmail

We have talked about Prado in past issues of PCQuest, 'Call it PHP .Net' and

'A Datagrid for PHP.' You can find these articles online at http://pcquest.ciol.

com/content/Developer/2004/ 104113004.asp; and http://pcquest.ciol. com/content/Deve

loper/2005/ 105041201.asp respectively. Besides facilitating UI based features

such as validators, datagrids and panels, Prado can also be used to deploy and

consume Web services. As a useful example, we can write a Web service using

Prado, to import contacts from a Gmail account. The parameters to the Web

service will be the Gmail username and password of the person, whose contacts

need to be retrieved.


PHP 5.1 on Apache on Linux or IIS on Windows. The latest version of Prado

(3.1.0.r2045 as of this writing) requires PHP 5.1 or above.

Besides, for Web services you require SOAP to be enabled in PHP. For importing
Gmail contacts we will be using libgmailer (


Libgmailer will require CURL support in PHP. That is, SOAP and CURL support

should be compiled into PHP (in case of Linux) or the corresponding DLLs should

be enabled in php.ini (in case of Windows).

Set up Prado and Libgmailer

Download Prado from www.pradosoft. com/download/?page=Download. Home.

Extract the zipped archive. Copy the directory named framework in the document

root of your Web server. Typically, the document root in case of Apache on Linux

(RedHat/Fedora based distros) will be /var/www/html and in case of IIS on

Windows, it will be c:\inetput\wwwroot.

Prado requires a defined directory structure for Web applications. Refer to

tutorial at the URL to
create this directory structure in the document root. Substitute webservice for

the name of the directory (which is helloworld in the tutorial).


Download libgmailer from

Extract the archive and copy the file named libgmailer.php to the directory


Web service class

Next we write a PHP class called GmailContacts which will have a method

(function) called getContacts()to retrieve Gmail contacts using libgmailer.php.

The code is as follows:



class GmailContacts



* Get Gmail Contacts

* @param string $login

* @param string $password

* @return mixed Gmail Contacts

* @soapmethod


public function getContacts($login,$password){

$gmailer = new GMailer();

if ($gmailer->created) {

$gmailer->setLoginInfo($login, $password, 0);

if ($gmailer->connect()) {

$gmailer->fetchBox(GM_CONTACT, "all", "");

$snapshot = $gmailer->getSnapshot(GM_CONTACT);








Save the above as GmailContacts.php in the directory webservice/protected/

pages. Note that the name of the file must match the name of the class (GmailContacts

in this case). Next, note the chunk:


* Get Gmail Contacts

* @param string $login

* @param string $password

* @return mixed Gmail Contacts

* @soapmethod



This chunk helps create the WSDL (Web Service Definition Language) file for a

Web service. It gives a description ('Get Gmail Contacts') to be used for the

web service, the input parameters type (string), the parameter names ($login,

$password), the return type (mixed) and the description of the data returned ('Gmail


Finally with @soapmethod you declare that this method can be used as a Web



Expose the Web service

With the GmailContacts class in place, we are now ready to expose this as a

web service. For this, open the file named application.xml, found in the

directory webservice/protected, in a text editor. Scroll down to the lines:

Insert the following lines between the and tag pair.


After inserting the above lines, the section should

look like this:

Note the ID of the service called 'soap.' The provider ID of the service is

gmailcontacts and the provider path is Application.pages.GmailContacts.

In Prado, Application refers to the root directory of the Web application,
'webservice' in our case. 'Pages' refers to the directory protected/pages in the

root directory. We can refer to a class, residing in the protected/pages

directory, using its name. Now, save the application.xml file.

Once this is done, you can view the WSDL file of the Web Service using the URL
in your Web browser (IE or Firefox):


Substitute with localhost or the IP address of the Windows/Linux

machine where the Web Service is hosted.

The syntax of the above URL is:

http:///webservice/index.php?= .wsdl

Here the and are the ones specified in the

application.xml file.

Libgmailer library retrieves

the Gmail pages (after authentication) and parses the HTML of the contacts

page (shown above) to retrieve Gmail contacts

Consume the service

To consume the Web Service, save the following code as WebServiceClient.php

in the directory webservice/protected/ pages:

class WebServiceClient extends TPage


function onLoad($param){

if(!$this->IsPostBack) {

$client=new SoapClient('http:///webservice/index.php? soap=gmailcontacts.wsdl');







Note the following statement:

$client=new SoapClient('


index.php? soap=gmailcontacts.


Here, the SoapClient constructor takes the WSDL URL (as mentioned above) as

the parameter. Now, look at the following line:


Here, substitute and with a valid Gmail

login and the corresponding password. For specify the Gmail

login without ''

When contacts are retrieved, they are shown in a datagrid on the page. Save

the following code as in the directory webservice/protected/


To see the client working, key in the following URL:

http:///webservice/ index.php?page=WebServiceClient

This will display a list of e-mails of all the Gmail contacts.

Note that the client can reside on a different machine and/or can be a

different Prado Web application.

This simple Web Services application exposes a plethora of possibilities. For

example, you can import your Gmail contacts in a CRM or a Groupware application.

Note that Gmail itself does not provide a Web Service to retrieve contacts.

So, such a Web Service can be a useful add-on to your integration efforts.