Core PHP, when downloaded from php.net 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 (www.pradosoft.com), Symfony (www.symfony-project.com) and CakePHP
(www.cakephp.org) 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: Developers USP: A simple (using a library) and scalable (through a web service) way to retrieve Gmail contacts Primary Link: www.pradosoft.com Keywords: 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.
Prerequisites
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 (http://gmail-lite.sourceforge.net/).
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
http://www.pradosoft.com/demos/quickstart/?page=GettingStarted.HelloWorld 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 http://gmail-lite.sourceforge.net/wordpress/index.php/download/.
Extract the archive and copy the file named libgmailer.php to the directory
webservice/protected/pages.
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:
include("libgmailer.php");
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);
return
$snapshot->contacts;
}
}
}
}
?>
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
Contacts').
Finally with @soapmethod you declare that this method can be used as a Web
Service.
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
After inserting the above lines, the
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):
http://
Substitute
machine where the Web Service is hosted.
The syntax of the above URL is:
http://
Here 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://
$this->DataGrid->DataSource=$client->getContacts("
$this->DataGrid->dataBind();
}
}
}
?>
Note the following statement:
$client=new SoapClient('
http://
index.php? soap=gmailcontacts.
wsdl');
Here, the SoapClient constructor takes the WSDL URL (as mentioned above) as
the parameter. Now, look at the following line:
$this->DataGrid->DataSource=$client->getContacts("
Here, substitute
login and the corresponding password. For
login without '@gmail.com.'
When contacts are retrieved, they are shown in a datagrid on the page. Save
the following code as WebServiceClient.page in the directory webservice/protected/
pages:
To see the client working, key in the following URL:
http://
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.