Google Docs API: Client Login with PHP and Curl

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 = "";
$clientlogin_post = array(
    "accountType" => "HOSTED_OR_GOOGLE",
    "Email" => "",
    "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_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, "");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);

$response = curl_exec($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.

40 thoughts on “Google Docs API: Client Login with PHP and Curl

  1. Tweets that mention Google Docs API: Client Login with PHP and Curl --

  2. 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"

  3. "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

  4. 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:….

    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='; xmlns:openSearch='; xmlns:docs='; xmlns: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

  5. 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.

  6. @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;

  7. 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?

  8. 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

  9. Google Docs API: Client Login with PHP and Curl (Notes)

  10. 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:


  11. Subir y exportar documentos usando Google Docs y PHP | Sentido Web

  12. Hello,
    I successfully used your code to get my authentication key, though while using it in conjuncture with:
    curl_setopt($curl, CURLOPT_URL, ';out:json&key=&#039;.$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 2 inu003c/au003e'}]});"

    Would you have an idea what is going on here?
    Thank you so much!

  13. 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

    • 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 ;)

  14. 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

Comments are closed.