Write to a google spreadsheet using php Script 

In this article,I will explain how to write to a google docs spreadsheet using php Script.
I have written simple PHP Script to create a new google spreadsheet using MYSQL Database data.
You can Write/Save any type of files(e.g. *.txt,*.doc,*.csv......etc) to a google Docs Spreadsheet using below Script.
Google Spreadsheet

Suppose you want to write to a Google Spreadsheet from a PHP script. Here's an example.

writeSpreadheet.php

<?php
 /*Author: Rajesh S(rajeshstutorials.blogspot.com)
  */
ini_set('max_execution_time', 3600);
ini_set("display_errors","1");

/************************************ Login Process at google Doc
*******************************************************/
// Construct an HTTP POST request
$client_url = "https://www.google.com/accounts/ClientLogin";
$client_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "test@gmail.com",
"Passwd" => "test",
"service" => "writely",
"source" => "Reports"
);
 // Initialize the curl object
$curl = curl_init($client_url);

// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $client_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."
";
/********************Login Process at google Doc close*******************************************************/
   $address_url="http://localhost/createxls.php";
 curl_setopt($curl, CURLOPT_URL, $address_url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
 $filesize=strlen($data);
$title = "test.tsv";

$headers = array(
 "Authorization: GoogleLogin auth=" . $auth,
 "GData-Version: 3.0",
 "Content-Length: ". $filesize,
 "Content-Type: text/plain",
 "Slug: ". $title
);
 curl_setopt($curl, CURLOPT_URL, "
https://docs.google.com/feeds/default/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
echo  $response."
";
curl_close($curl);

?>



createxls.php


<?php
$host="localhost";
$username="root";
$password="";
$dbname="mydb";

$conn = mysql_connect($host,$username,$password,true);// Connection with
localhost
$db = mysql_select_db($dbname, $conn) or die ("Couldn't select database.");

header("Content-Type: text/plain");

$flag = false;

$result="SELECT  `first_name`, `email`, `mobile` FROM `employees` ";

$query=mysql_query($result,$conn);
if(mysql_num_rows($query)>0)
{
 while(($rows = mysql_fetch_assoc($query)))
{
$row = array
(
"EmpName" => $rows['first_name'], "Email" => $rows['email'], "Mobile" =>
$rows['mobile']
);
 if(!$flag)
{
 echo implode("\t", array_keys($row)) . "\n";
    $flag = true;
}
echo implode("\t", array_values($row)) . "\n";
 }
}
else
{
echo "No data found";
}

?>


I hope this example will help you to write google Spreadsheets.


Cheer :)