A few days ago I started looking deeper into the Google Code APIs and threw a few experiments using the Google Documents List Data API. Unfortunately, the only library they have for the third version of their protocol is written in Java. There is a PHP wrapper for the first version of the protocol, but it totally depends on the Zend Framework. Here’s a little code snippet for logging into a Google Docs account (writely) using ClientLogin with Curl and PHP. The Auth string is stored into the $auth variable for later use.
// Construct an HTTP POST request $clientlogin_url = "https://www.google.com/accounts/ClientLogin"; $clientlogin_post = array( "accountType" => "HOSTED_OR_GOOGLE", "Email" => "yourgoogle@email.com", "Passwd" => "yourgooglepassword", "service" => "writely", "source" => "your application name" ); // Initialize the curl object $curl = curl_init($clientlogin_url); // Set some options (some for SHTTP) curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Execute $response = curl_exec($curl); // Get the Auth string and save it preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches); $auth = $matches[1]; echo "The auth string is: " . $auth;
ClientLogin assumes you login once and use the auth string for all on-going requests (more info: ClientLogin – Google Code). Here’s a simple example of how to retrieve some data from a Google Docs account, show filename, author and file type. Make sure the simplexml php extension is installed and activated.
// Include the Auth string in the headers // Together with the API version being used $headers = array( "Authorization: GoogleLogin auth=" . $auth, "GData-Version: 3.0", ); // Make the request curl_setopt($curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full"); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, false); $response = curl_exec($curl); curl_close($curl); // Parse the response $response = simplexml_load_string($response); // Output data foreach($response->entry as $file) { echo "File: " . $file->title . "<br />"; echo "Type: " . $file->content["type"] . "<br />"; echo "Author: " . $file->author->name . "<br /><br />"; }
It seems though that Google Docs is purely for documents (spreadsheets and presentations) but not for actual files, as a JPEG I uploaded turned into a text/html type. It opens up in the text editor when viewing in Google Docs, and the nearest-to JPEG export format is PDF (just like all the other documents). So I guess there’s no way of reading and manipulating the raw image as if I would read and manipulate a file.
I came across a cool website which I believe the Googlers have made – Data Liberation:
Users should be able to control the data they store in any of Google’s products. Our team’s goal is to make it easier for them to move data in and out.
I’m working on a little project that involves image (and file) hosting, so the products I’m interested in are Google Docs and Picasa Web Albums. It seems that none of the two make it possible to store and work with files as if it was a flash disk or an FTP server. Google Docs is the closest one but there’s a huge limit on what kind of files you can upload and store. Picasa on the other hand is good for image storing, but the only way to export a set of images is to use their desktop software and Picasa Web, and of course no actual control over the files (edit, delete) via their Picasa Web API. If only it were a little bit closer to Amazon S3 …
Oh and you should probably use OAuth or AuthSub when working with Web Applications and Google APIs but anyways, this was just a quick example.
[…] This post was mentioned on Twitter by Timothy Post and hariharank12, Michael Davis. Michael Davis said: Google Docs API: Client Login with PHP and Curl http://bit.ly/3geSOA […]
Great article! Thanks for the code.
for me works with this line before the second curl_exec :
curl_setopt($curl, CURLOPT_POST, false);
Right, I assumed you used the same curl object for the second piece of code as well, as there's the setop disable post in the first piece ;)
he is right, you need to have this line:
curl_setopt($curl, CURLOPT_POST, false);
right above $response = curl_exec($curl);
otherwise it just responds with errors saying "GData rateLimitExceeded"
"Google Docs is the closest one but there’s a huge limit on what kind of files you can upload and store."
not true anymore…. you can now upload any file to google docs
True, limited to 100M ;)
Hello!
thanks for the example, but when I execute it I always get the following error:
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /var/www/php/google_docs.php on line 53
Warning: simplexml_load_string() [function.simplexml-load-string]: 1 in /var/www/php/google_docs.php on line 53
Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /var/www/php/google_docs.php on line 53
And if I display the content of the response with echo $response, the response is something like this:
http://docs.google.com/feeds/default/private/full….
but it is not a valid xml, so the parser fails. But if I display the source code of the page it is something like this:
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:g…..
Please, what am I doing wrong?.
Thanks a lot and regards, Iván.
Hi Ivan, not sure what the issue is here, but it seems that your request is wrong. Please go through the API docs, also note what @required said above.
~ K
Hello,
I have a problem with your example. The line $response = curl_exec($curl); makes that the response gets displayed on the browser and when I parse the result I get the error: "Entity: line 1: parser error : Start tag expected, '<' not found i….."
How I can resolve this problem?.
Thanks and regards, Iván.
hi.., how to wrapper google search … it contain javascript … please help me ..
@ivan
curl_exec outputs response to browser by default. You need to set the following curl option:
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
That way the curl response will be saved into $response;
@Calvin: Thanks a lot!. It worked for me :-)
Thanks for the code! I've been racking my brain for hours. I have it working and it brings back everything. Is it possible to limit to certain directory's?
Hello,
I am a Computer Science student and i intend to do a project using php and google docs v3.0 … so do you think i can use all google docs features by writing my own php code or it will be hard ….we have about month and half for finishing that project
I think you'll be okay ;)
[…] Google Docs API: Client Login with PHP and Curl This entry was posted on Monday, May 24th, 2010 at 11:38 amand is filed under Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. […]
works great for docs.google.com, but i am getting an Invalid Token error when I set the URL in the second example to http://spreadsheets.google.com/feeds/cells/$key/1… (replace $key with your spreadsheet's key).
Perhaps you have to auth for all domains?
hi i want to create a session with clientlogin is this possible?? thanks
I hope your answer
Yes it is :D
It works perfect but the documents last updated stamp is not matching. like it's picking up a different timezone?
Today is 6-30 and this is the stamp for one file:
2010-07-01T02:04:48.137Z
Yeah, perhaps you need some local timezone configuration. Or Google docs configuration if you're the admin.
[…] Vía / Google Docs API: Client Login with PHP and Curl […]
Hello,
I successfully used your code to get my authentication key, though while using it in conjuncture with:
curl_setopt($curl, CURLOPT_URL, 'https://spreadsheets.google.com/tq?tqx=version:0.6;out:json&key='.$key);#
($key being my spreadsheet key)I get the response:
string(272) "google.visualization.Query.setResponse({version:'0.6',status:'error',errors:[{reason:'user_not_authenticated',message:'User not signed in',detailed_message:'u003ca target=u0022_blanku0022 href=u002 2http://spreadsheets.google.com/u0022u003eSign inu003c/au003e'}]});"
Would you have an idea what is going on here?
Thank you so much!
Thanx dude, it worked like a charm
Hello,
First i'd like to thank you for this awesome post..
and now i have to questions:
1- can i make the output data (name of the doc: $file->title) as a link to the specific doc?
2- can i output the name of the folders also?
thanks mate
André
Andre, use <code>print_r</code> to inspect what's inside the objects and arrays returned. They might contain the folders too, although I'm not sure, haven't used this for quite some time, things change ;)
Hi, Great code. I am trying to do the same thing wit the Shopping API and get the token, but what is the url of the shopping url to put into the header vars?
Thanks for your time
James
Not sure what you're talking about James, try and hit me on Skype we can chat about it.
Can google API allow to me read MS Word documents in my site?
Sife, I guess, but that's a different thing, look on how to embed docs.
I got the 403.4 SSL required error. what's the problem?
Thanks in advance.
alright, just find the solution: should use https instead of http this line:
curl_setopt($curl, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full");
yeah thanks man! this solved my problem :-)
Am getting GDataServiceForbiddenException403.4 SSL required exception
I got it…
I missed https in upload document.
By using https i got this..
Glad you sorted it out :)
Got the same error as anu, SSL error. Same question too: what's the problem?
Thanks in advance
Try turning off SSL verification for Curl.