Hello,
I have been trying to add rows to one of my spreadsheets using cURL
for PhP. I always get a "We're sorry, a server error occurred.
Please wait a bit and try reloading your spreadsheet." or a "Blank
rows cannot be written; use delete instead" when dumping the response
from the server. I'm not sure what I am missing.
I'm using ClientLogin to get the token used in every HTTP request.
Here is my code to add a row:
CLIENTLOGIN CODE:
----------------------------------------------------------------------------------------------------
static function ClientLogin( $email, $pword, $service){
$accType = "HOSTED_OR_GOOGLE";
$source="company-Applications-1.0";
$req_url = "https://www.google.com/accounts/ClientLogin";
$login_params=array(
"accountType" => $accType
, "Email" => $email
, "Passwd" => $pword
, "service" => $service
, "source" => $source
);
$curl= curl_init( $req_url );
$options=array(
CURLOPT_POST => true
, CURLOPT_POSTFIELDS => $login_params
//, CURLOPT_HTTPHEADER => $header
, CURLOPT_HTTPAUTH => CURLAUTH_ANY
, CURLOPT_SSL_VERIFYHOST => true
, CURLOPT_SSL_VERIFYPEER => false
, CURLOPT_RETURNTRANSFER => 1
);
curl_setopt_array( $curl, $options );
$response = curl_exec( $curl );
$info = curl_getinfo($curl);
preg_match( "/Auth=([a-z0-9_\-]+)/i", $response, $matches );
$auth = $matches[1];
return $auth;
}
------------------------------------------------------------------------------
ADD ROW CODE:
static function addRow( $Fname, $Lname, $email, $phone, $company,
$msg, $auth ){
$header = array(
"Authorization: GoogleLogin auth=" . $auth
,"GData-Version: 3.0"
,"content-type: application/atom+xml"
);
$post_URL = "https://spreadsheets.google.com/feeds/list/(KEY
VALUE GOES HERE)/1/private/full";
$curl = curl_init( $post_URL );
$myXml="<?xml version='1.0' encoding='ISO-8859-1'?>
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:gsx='http://schemas.google.com/spreadsheets/2006/
extended'>
<gsx:FirstName>$Fname</gsx:FirstName>
<gsx:LastName>$Lname</gsx:LastName>
<gsx:Email>$email</gsx:Email>
<gsx:Phone>$phone</gsx:Phone>
<gsx:CompanyName>$company</gsx:CompanyName>
<gsx:Message>$msg</gsx:Message>
</entry>";
$options=array(
CURLOPT_POST => true
, CURLOPT_POSTFIELDS => $myXml
, CURLOPT_HTTPHEADER => $header
, CURLOPT_HEADER => true
, CURLOPT_HTTPAUTH => CURLAUTH_ANY
, CURLOPT_SSL_VERIFYHOST => true
, CURLOPT_SSL_VERIFYPEER => false
, CURLOPT_RETURNTRANSFER => 1
, CURLOPT_FOLLOWLOCATION => true // follow
redirects
, CURLOPT_ENCODING => "" // handle all
encodings
, CURLOPT_AUTOREFERER => true // set referer on
redirect
, CURLOPT_CONNECTTIMEOUT => 120 // timeout on
connect
, CURLOPT_TIMEOUT => 120 // timeout on
response
, CURLOPT_MAXREDIRS => 10
);
curl_setopt_array( $curl, $options );
$google_sheet = curl_exec( $curl );
$info = curl_getinfo($curl);
curl_close( $curl );
}
}
Any help would be appreciated. Thanks!