使用php/curl将数据发布到Google Apps脚本 [英] Posting data to google apps script using php/curl

查看:90
本文介绍了使用php/curl将数据发布到Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从基于php的数据库系统向Google Apps引擎提交一些简单数据.

I want to submit some simple data from a php based database system to google apps engine.

以下代码不会产生任何错误,但也不会产生任何结果.我不知道为什么Google Apps脚本不响应.

The following code produces no error, but no result, too. I can't find out why google apps script does not react.

将google apps脚本配置为供知道网址的任何人使用(允许匿名用户).该脚本将由我的帐户执行,即使该脚本是由匿名用户触发的也是如此. apps脚本具有工作所需的所有权限.

The google apps script is configured for use by anybody who knows the url (anonymous user is allowed). The script will be executed by my account, even if it is triggered bei anonymous user. The apps script has all the rights it needs to work.

目标是google apps脚本显示发布的数据.

The goal is that google apps script shows the posted data.

任何帮助都将非常受欢迎.也许还有另一种将数据传输到Google Apps脚本的方法. 经过数小时的搜索和尝试,我找不到解决方案.

Any help will be very welcome. Maybe there is another method to transfer the data to google apps script. I could'nt find a solution after several hours of searching and trying.

在PHP系统端,我具有以下代码来发送数据:

On the PHP-System-side I've got the following code to SEND data:

// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://script.google.com/macros/s/AKfycbykWm97cwsxoP6NGhub8AqGuQammVwCwVgJrxjFLxY8TAFGRw/exec',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
    'fname' => 'John',
    'lname' => 'Miller'
)
));

// Send the request, save response to $resp
$resp = curl_exec($curl);

// Show response
echo $resp;

// Close request to clear up some resources
curl_close($curl);

echo 'curl closed';

在Google Apps脚本方面,我具有以下代码来接收数据:

On the Google apps script-side I've got the following code to RECEIVE data:

// Show name in google apps


function doPost(e) {

  var parameter
  var value

  for (var i in e.parameters) {
    parameter = i;
    wert =  e.parameters[i];

    if (parameter == "lname") {var lname = value;}    
    if (parameter == "fname") {var fname = value;}

  }

  //Return  
  return HtmlService.createHtmlOutput("The name is: " + fname + " " + lname)

}

推荐答案

请勿使用htmlService,请使用contentService并返回json或纯文本.官方文档中对此有更详细的解释 如果仍然无法确认,请确保您的php脚本可以处理对googleusercontent.com的重定向.首先使用doGet在浏览器的url上尝试一下.您也可以在PHP中使用get代替post.

Do not use htmlService, use contentService and return a json or plain text. This is explained in more detail in the official docs if still doesnt aork make sure your php script can handle a redirect to googleusercontent.com. try it on the browser url using doGet first. You can also use get instead of post in your php.

这篇关于使用php/curl将数据发布到Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆