【问题标题】:How to send an e.parameter to Google Apps Script using PHP and cURL如何使用 PHP 和 cURL 将 e.parameter 发送到 Google Apps 脚本
【发布时间】:2020-05-11 14:50:11
【问题描述】:

下面的代码有效。它使用e.queryString 请求参数将数据发送到谷歌应用程序脚本。我想知道如何更新它,以便它发送相同的数据,但使用对象和 e.parameter 参数。

如何更新我的工作代码,以便它将以下对象 e.parameter 发送到我的脚本 @https://script.google.com/macros/s/XXXXXXSCRIPTIDXXXXX/exec 并将值返回到我的 php 文件?

对象

{"responderName": "todd", "presenterName": "steve"}

Google Documentation

使用 e.queryString 参数的工作 PHP 代码

$url = 'https://script.google.com/macros/s/XXXXXXSCRIPTIDXXXXX/exec?responderName=todd&presenterName=steve';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($curl);
echo $response;
curl_close($curl);

使用 e.queryString 的工作应用程序脚本代码

function doGet(e) {
  var params = JSON.stringify(e);
  return ContentService.createTextOutput(params);
}

【问题讨论】:

  • 不清楚。你想发送一个对象而不是一个查询字符串?
  • @TheMaster,很抱歉这个问题的措辞不正确。答案是使用。我想发送一个对象而不是查询字符串。感谢您的帮助!
  • 事件对象已经有一个parameter 和一个parameters 属性。 Google 从 URL 参数 developers.google.com/apps-script/guides/web#request_parameters 构造在您的 doGet 方法中接收到的对象添加此 sn-p 后,在 Stackdriver 日志中查看 e:console.log({ message: "doget e", eventObject: e })
  • @tehhowch,谢谢。此信息帮助我了解我的问题措辞错误。我试图弄清楚如何在不使用 URL 中的查询字符串的情况下将信息发送到 Google Apps 脚本。是否可以在不将数据添加到 url 的情况下发送对象?

标签: javascript php curl google-apps-script


【解决方案1】:
  • 您希望在不使用 URL 查询参数的情况下将 responderName=todd&presenterName=steve 的数据发送到 Web 应用程序。
  • 您的 Web 应用程序已部署为 Execute the app as: MeWho has access to the app: Anyone, even anonymous
  • 您想使用 PHP 的 curl 发送数据。

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

问题和解决方法:

当您想在不使用 URL 查询参数的情况下将数据发送到 Web Apps 时,需要使用 POST 方法。所以在你的情况下,请使用doPost() 而不是doGet()。这样,数据就可以作为POST方法接收了。

修改后的脚本:

Google Apps 脚本:

请按如下方式修改您的 Google Apps 脚本。

function doPost(e) { // Modified
  var params = JSON.stringify(e);
  return ContentService.createTextOutput(params);
}
  • 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。由此,反映最新的脚本。请注意这一点。

PHP:

请按如下方式修改您的 PHP 脚本。

$data = array(
    'responderName' => 'todd',
    'presenterName' => 'steve'
);

$url = 'https://script.google.com/macros/s/###/exec';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$header = ['Content-Type: application/json'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
echo $response;
curl_close($curl);
  • 在这种情况下,可能不需要使用标头。

结果:

当使用上述 PHP 脚本时,可以在 doPost(e)e 检索到以下事件对象。在这种情况下,可以通过e.postData.contents 检索数据。但是在这种情况下,如果您想将数据用作 JSON 对象,请使用JSON.parse()

{
  "parameter": {},
  "contextPath": "",
  "contentLength": 48,
  "queryString": "",
  "parameters": {},
  "postData": {
    "type": "application/json",
    "length": 48,
    "contents": "{\"responderName\":\"todd\",\"presenterName\":\"steve\"}",
    "name": "postData"
  }
}

参考资料:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

【讨论】:

  • @Mr. B 谢谢你的回复。我很高兴你的问题得到了解决。也谢谢你。
  • 你在这里帮助了我好几次,我真的很感谢你!祝您度过愉快的一周。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多