views:

503

answers:

3

How can I save data in unpublic google spreadsheet in PHP without using Zend Gdata lib? I can't use Zend libs, as it's shown on google tutorials, because the php server on which I'm running the script is php v. 5.0.4. I tried to find a solution using cUrl, but I can't omit the problem with authentication when doc is unpublic. How can I do it? If anyone has attempted it, please share the solution.

A: 

You can access the GData APIs using ordinary HTTP requests; a library just makes this easier for you. You basically just end up rewriting the bits of the library you want to use.

See the protocol documentation.

Wooble
+2  A: 

I have found out solution using curl and by creating a form for the google's spreadsheet. For prepared spreadsheet you have to create a form, without options: Require sign-in to view this form and Automatically collect respondent's username. Then check, using f.e. firebug, the form post uri and post data and use it to following script:

#prepare post data
$fields = array('backupCache' => '',
            'entry.0.single'=>urlencode($data['name']),
            'entry.1.single'=>urlencode($data['surname']),
            'pageNumber'=>urlencode(0),   
            'submit'=>'Submit');
$fields_string = '';

foreach($fields as $key=>$value) {
  $fields_string .= $key.'='.$value.'&';
}

rtrim($fields_string,"& ");
$fields_string = substr($fields_string, 0, strlen($fields_string)-1);

$ch = curl_init();
#set curl_setopt for your preferences
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);    

curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

#set proper form uri
curl_setopt($ch, CURLOPT_URL, $formUri);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

$res = curl_exec($ch);
curl_close($ch);

May not be the most perfect solution but it works. :)

Magda
+1  A: 

Here is an object-oriented php class for sending data to a Google Docs Spreadsheet - http://code.google.com/p/php-form-builder-class/source/browse/trunk/includes/class.spreadsheet.php?spec=svn384&r=384

cUrl is used instead of Zend's GData lib. You can find a sample implementation below. Remember to replace the test settings ("my_google_email", "my_google_password", etc) with your specific information.

$doc = new spreadsheet();
$doc->authenticate("my_google_email", "my_google_password");
$doc->setSpreadsheet("my_spreadsheet_title");
$doc->setWorksheet("my_worksheet_title");
$my_data = array("First Name" => "John", "Last Name" => "Doe");
$doc->add($my_data);

The keys of the associative array passed to the add method need to match the column headers of the spreadsheets you're using to collect the data.