forked from aeknarinamn/PHPGoogleAPIsSheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleSheetsAPI.php
More file actions
156 lines (126 loc) · 3.97 KB
/
googleSheetsAPI.php
File metadata and controls
156 lines (126 loc) · 3.97 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
<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
/*Get Data From POST Http Request*/
$datas = file_get_contents('php://input');
/*Decode Json From LINE Data Body*/
$deCode = json_decode($datas,true);
file_put_contents('log.txt', file_get_contents('php://input') . PHP_EOL, FILE_APPEND);
$replyToken = $deCode['events'][0]['replyToken'];
$userId = $deCode['events'][0]['source']['userId'];
$type = $deCode['events'][0]['type'];
$token = "phw/2KAuGB9u33Q2DhR8DapdPgtO9hXrIJePusIlU/3obGyG/zcJjgBV6qIGVy0chUznVjW/Ixck/lNbwExb7kgMY616rfK55V9nbSRYOEMKmYciTPXX1rP3QgGsz+2DOV+LWtbzr1iSItKvobRK9QdB04t89/1O/w1cDnyilFU=
";
$LINEProfileDatas['url'] = "https://api.line.me/v2/bot/profile/".$userId;
$LINEProfileDatas['token'] = $token;
$resultsLineProfile = getLINEProfile($LINEProfileDatas);
$LINEUserProfile = json_decode($resultsLineProfile['message'],true);
$displayName = $LINEUserProfile['displayName'];
/*
* We need to get a Google_Client object first to handle auth and api calls, etc.
*/
$client = new \Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(\Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig(__DIR__.'/impact-chatbot-2019-hhteta-5bb6ff037d61.json');
$client->setAccessType('offline');
// $client->setPrompt('select_account consent');
$service = new \Google_Service_Sheets($client);
$spreadsheetId = "1PQtBL97j6GCQeoePdqc7YDXcoGET_q-8uPuL_5xPkhE";
// updateData($spreadsheetId,$service);
insertData($spreadsheetId,$service,$displayName);
function insertData($spreadsheetId,$service,$displayName)
{
// $range = 'congress!D2:F1000000';
//INSERT DATA
$range = 'a2';
$values = [
[$displayName],
];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$insert = [
'insertDataOption' => 'INSERT_ROWS'
];
$result = $service->spreadsheets_values->append(
$spreadsheetId,
$range,
$body,
$params,
$insert
);
}
function updateData($spreadsheetId,$service)
{
$range = 'a2:b2';
$values = [
["Test","Test"],
];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$result = $service->spreadsheets_values->update(
$spreadsheetId,
$range,
$body,
$params
);
}
function getData($spreadsheetId,$service)
{
// GET DATA
$range = 'congress!D2:F1000000';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if(empty($values)){
print "No Data Found.\n";
}else{
foreach ($values as $row) {
echo $row[0]."<br/>";
}
}
}
function getLINEProfile($datas)
{
$datasReturn = [];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $datas['url'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$datas['token'],
"Postman-Token: 32d99c7d-9f6e-4413-a4d2-fa0a9f1ecf6d",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$datasReturn['result'] = 'E';
$datasReturn['message'] = $err;
} else {
if($response == "{}"){
$datasReturn['result'] = 'S';
$datasReturn['message'] = 'Success';
}else{
$datasReturn['result'] = 'E';
$datasReturn['message'] = $response;
}
}
return $datasReturn;
}