I find it quite hard having a comprehensive sample or documentation on using Google data services with bare PHP code. Finally - with some trials and errors - can get things done and now fully understand how to interact with
GData Protocol.
One of the basic ways to interact with Google data services is to use
ClientLogin, in which your web application or desktop application can authenticate into Google services using raw http and xml
protocol.
So, based on Google
documentation at how to use curl, I have written some PHP codes to test against each service. One that had been fully tested is
Picasa.
There are several issues when I try to use ClientLogin mechanism in PHP :
- The application doesn't know the SSL key and if we can add it dynamically, it will be limited if you are using shared host environment.
- You have to construct your own HTTP header as authentication process part.
Thanks to curl library function that can overcome all the issues. So here are my codes that I think is quite intuitive and understandable.
But please mind that you need to understand GData
protocol first before you continued on. And you will need of course, a Picasa account and some uploaded photos.
Obtaining Login Token
Here is a sample code on how to login into Picasa service (code :
hl2). For other services code see
here. Replace
Email and
Passwd entry with your own gmail address and password.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array('accountType' => 'GOOGLE',
'Email' => 'youremailaddress@gmail.com',
'Passwd' => 'yourpassword',
'source'=>'PHI-cUrl-Example',
'service'=>'lh2');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$hasil = curl_exec($ch);
echo $hasil;
?>
Sample output format will looks like below. For the sake of simplicity, all token's long text are truncated.
SID=DQA...oUE
LSID=DQA...bbo
Auth=DQA...Sxq
You will use this Auth token for Picasa resources that is not publicly consumed. This token will last for
24 hours.
Using the service
And here I show you how to consume the service based on this
reference. For this sample I use url service :
http://picasaweb.google.com/data/feed/base/user/PicasaUserID/album/PicasaAlbumID?imgmax=200.
Replace PicasaUserID and PicasaAlbumID with your real Picasa user and album id.
This will return feeds that contains information and image url of your album entries. Make sure that your album is set to unlisted album or not a public one.
The complete php source code is shown below.
<html>
<head>
<title>Picasa GData API Demo - PHP CURL</title>
<style>
.container td {
padding: 0px;
border:1px solid black;
}
.container td td {
padding: 0px;
border: 0px;
}
</style>
</head>
<body>
<?php
$ch = curl_init("http://picasaweb.google.com/data/feed/base/user/PicasaUserID/album/PicasaAlbumID?imgmax=200");
$header[] = 'Authorization: GoogleLogin auth=DQA...Sxq';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$hasil = curl_exec($ch);
curl_close($ch);
if(preg_match("@<\?xml version='1\.0' encoding='UTF-8'\?>@",$hasil))
{
$X = simplexml_load_string($hasil);
echo "<table cellspacing='0' cellpadding='0' class='container' width='100%'>";
echo "<tr style='color:white; background: black;'>";
echo "<td width='400px'>Summary</td><td align='center'>200pixel width</td></tr>";
foreach ($X->entry as $a){
echo("<tr>");
echo("<td>" . $a->summary . "</td>");
foreach($a->content->attributes() as $b=>$c)
{
if($b=="src"):
echo "<td align='center'><img src='$c'></td>";
endif;
}
echo "</tr>";
}
}
else
{
echo $hasil;
}
?>
</body>
</html>
Notice that we pass our token to
header[] array and have it set with
curl_setopt($ch, CURLOPT_HTTPHEADER, $header) function.
If all running well, you will see something like in the picture below. Here I have table of two images with a summary of each photo and a thumbnail of 200 pixel width.
Hope that you may find this article useful.
Feris Thia
Business Intelligence Consultant
http://www.phi-integration.com