Marco
Marco

Reputation: 1492

Create empty spreadsheet in Google Docs via cURL and PHP

I am trying to create a new spreadsheet in Google docs without using the Zend library. I worked through the documentation and tried to follow the steps but now I am stuck. My code looks like this

    $curl = curl_init();

    $headers = array(
        "GData-Version: 3.0",
        "Authorization: GoogleLogin auth=" . $myToken,
        "Content-Length: 350",
        "Content-Type: application/atom+xml",
        "X-Upload-Content-Length: 0"
    );      

    $post_field = '<?xml version="1.0" encoding="UTF-8"?>
        <entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">
        <category scheme="http://schemas.google.com/g/2005#kind"
            term="http://schemas.google.com/docs/2007#spreadsheet"/>
        <title>Mytitle</title>
        </entry>';

    curl_setopt($curl, CURLOPT_URL, 'https://docs.google.com/feeds/upload/create-session/default/private/full');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_field);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);      

    $response = curl_exec($curl);
    curl_close($curl);

The token seems to be working fine since I am able to use it to retrieve data from google docs. What surprises me is that I don't get a response from the API. While trying out different solutions I usually got an error message, but here the response string is empty.

Upvotes: 0

Views: 1236

Answers (2)

Ali Afshar
Ali Afshar

Reputation: 41663

If you can, you should use the files.insert method of the Google Drive API instead.

Upvotes: 1

Claudio Cherubino
Claudio Cherubino

Reputation: 15024

You are sending a request to initiate a resumable upload session (https://developers.google.com/gdata/docs/resumable_upload) but never send the actual content.

Resumable upload is not needed when creating an empty file, instead send your request to https://docs.google.com/feeds/default/private/full

Upvotes: 2

Related Questions