我正在尝试使用curl php发布数据,但是我面临的问题是Google表格中保存的数据始终未定义。我已经使用了下面的代码,该代码附带了说明,还附加了图像网址,该网址显示了来自php脚本的值。我正在使用php和curl发送数据,并尝试将这些数据保存在Google工作表中。
图片显示未定义的值:https://ibb.co/0n5WKdz
PHP脚本
function send_to_url($url, $data)
{
$ch = curl_init($url);
//Set a POST method
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_TIMEOUT, 6);
//Set data to JSON application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$jsonDataEncoded = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//POST to URL
$result = curl_exec($ch);
//Get result from POST if any
$result_post = urldecode(json_encode($result));
curl_getinfo($ch);
curl_close($ch);
return $result;
}
$users =
array("Date"=>"Timestamp","FirstName"=>"John","LastName"=>"Cena","Email"=>"john78@example.com");
send_to_url("GOOGLE_APP_SCRIPT_URL",$users);
GOOGLE SCRIPT
var SHEET_NAME = "DATA";
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service
// If you don't want to expose either GET or POST methods you can comment out the appropriate function
function doGet(e){
return handleResponse(e);
}
function doPost(e){
return handleResponse(e);
}
function handleResponse(e) {
var lock = LockService.getPublicLock();
lock.waitLock(30000); // wait 30 seconds before conceding defeat.
try {
// next set where we write the data - you could write to multiple/alternate destinations
var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
var sheet = doc.getSheetByName(SHEET_NAME);
// we'll assume header is in row 1 but you can override with header_row in GET/POST data
var headRow = e.parameter.header_row || 1;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [];
// loop through the header columns
for (i in headers){
if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
row.push(new Date());
} else { // else use header name to get data
row.push(e.parameter[headers[i]]);
}
}
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
// return json success results
return ContentService
.createTextOutput(JSON.stringify({"result":"success", "row": headRow}))
.setMimeType(ContentService.MimeType.JSON);
} catch(e){
// if error return this
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
} finally { //release lock
lock.releaseLock();
}
}
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
SCRIPT_PROP.setProperty("key", doc.getId());
}
请检查我的代码并提供解决方案,让我知道我在哪里出错。
答案 0 :(得分:0)
在您的脚本中,我认为您的php脚本有效,并且需要对Google Apps脚本进行一些修改。那修改呢?
当JSON对象({"key1": "value1", "key2": "value2"}
)作为POST请求发送到Web Apps时,e
中的doPost(e)
如下所示。
{
"parameter": {},
"contextPath": "",
"contentLength": 36,
"queryString": "",
"parameters": {},
"postData": {
"type": "application/json",
"length": 36,
"contents": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
"name": "postData"
}
}
这样,当您想从对象中检索值时,就需要像var postData = JSON.parse(e.postData.contents)
那样对对象进行解析。看来,尽管数据是以application/json
的形式发送的,但该对象并未自动解析。
关于if (headers[i] == "Timestamp"){
行,您的示例图像没有Timestamp
。我以为可能是Date
。
请进行如下修改。
从:var row = [];
// loop through the header columns
for (i in headers){
if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
row.push(new Date());
} else { // else use header name to get data
row.push(e.parameter[headers[i]]);
}
}
至:
var postData = JSON.parse(e.postData.contents);
var row = headers.map(function(e) {return e == "Date" ? new Date() : postData[e]});
当您运行php脚本时,这些值将按如下所示放入电子表格中。
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。