forked from BackCheck/backcheck.io.verify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-sheets.php
More file actions
156 lines (146 loc) · 5.5 KB
/
google-sheets.php
File metadata and controls
156 lines (146 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
//make sure you include this file and call the constructor of class
class googlesheet {
private $token;
private $spreadsheet;
private $worksheet;
private $spreadsheetid;
private $worksheetid;
public function __construct() {
}
//constructed on call
public function authenticate($username, $password) {
$url = "https://www.google.com/accounts/ClientLogin";
$fields = array("accountType" => "HOSTED_OR_GOOGLE", "Email" => $username, "Passwd" => $password, "service" => "wise", "source" => "pfbc");
//this end constructor as in google documentation
$curl = curl_init();
//avoid blank lines or it will crash
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
//get response... same status code as on ajax xhtml
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($status == 200) {
if (stripos($response, "auth=") !== false) {
preg_match("/auth=([a-z0-9_\-]+)/i", $response, $matches);
$this -> token = $matches[1];
}
}
}
//preg_match is a key of error handle in this case
public function settitleSpreadsheet($title) {
$this -> spreadsheet = $title;
}
//finished setting the title
public function settitleWorksheet($title) {
$this -> worksheet = $title;
}
//choosing the worksheet
public function add_row($data) {
if (!empty($this -> token)) {
$url = $this -> getPostUrl();
if (!empty($url)) {
$headers = array("Content-Type: application/atom+xml", "Authorization: GoogleLogin auth=" . $this -> token, "GData-Version: 3.0");
//need to retreive the id of collums
$columnIDs = $this -> getColumnIDs();
if ($columnIDs) {
$fields = '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">';
foreach ($data as $key => $value) {
$key = $this -> formatColumnID($key);
//print_r($columnIDs)."<br>";
//print_r($key);
//die;
//if (in_array($key, $columnIDs)){
$fields .= "<gsx:$key><![CDATA[$value]]></gsx:$key>";
//}
}
$fields .= '</entry>';
//end constructor
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
}
}
}
}
//helper functions ...as defined in gogle api
private function getColumnIDs() {
$url = "https://spreadsheets.google.com/feeds/cells/" . $this -> spreadsheetid . "/" . $this -> worksheetid . "/private/full?max-row=1";
$headers = array("Authorization: GoogleLogin auth=" . $this -> token, "GData-Version: 3.0");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
//url are for 3.0... should change on depracated
if ($status == 200) {
$columnIDs = array();
$xml = simplexml_load_string($response);
if ($xml -> entry) {
$columnSize = sizeof($xml -> entry);
for ($c = 0; $c < $columnSize; ++$c)
$columnIDs[] = $this -> formatColumnID($xml -> entry[$c] -> content);
}
return $columnIDs;
}
//no return functions exits sillent
return "";
}
//need the url for post push method
private function getPostUrl() {
$url = "https://spreadsheets.google.com/feeds/spreadsheets/private/full?title=" . urlencode($this -> spreadsheet);
$headers = array("Authorization: GoogleLogin auth=" . $this -> token, "GData-Version: 3.0");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//let's compare statuses
if ($status == 200) {
$this -> worksheet="";
$spreadsheetXml = simplexml_load_string($response);
if ($spreadsheetXml -> entry) {
$this -> spreadsheetid = basename(trim($spreadsheetXml -> entry[0] -> id));
$url = "https://spreadsheets.google.com/feeds/worksheets/" . $this -> spreadsheetid . "/private/full";
if (!empty($this -> worksheet))
$url .= "?title=Sheet1";
//setopt
curl_setopt($curl, CURLOPT_URL, $url);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status == 200) {
$worksheetXml = simplexml_load_string($response);
//print_r($worksheetXml -> entry);die;
if ($worksheetXml -> entry)
$this -> worksheetid = basename(trim($worksheetXml -> entry[0] -> id));
}
}
}
curl_close($curl);
if (!empty($this -> spreadsheetid) && !empty($this -> worksheetid))
return "https://spreadsheets.google.com/feeds/list/" . $this -> spreadsheetid . "/" . $this -> worksheetid . "/private/full";
return "";
}
//no return
//need to do a preformat on id of collumn
private function formatColumnID($val) {
return preg_replace("/[^a-zA-Z0-9.-]/", "", strtolower($val));
}
}
?>