使用curl从PHP调用Google App脚本?

时间:2014-04-15 14:36:26

标签: curl google-apps-script

我正在尝试从PHP脚本调用我的Google App,这是代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, "https://script.google.com/macros/s/.../exec");
$res = curl_exec($ch); // $res = true

这会调用网址,并显示Google App脚本中的“正在加载”屏幕,就这样,没有其他任何事情发生。从浏览器调用URL工作,因此它必须与curl相关。也许我必须设置另一个卷曲选项?

谢谢!

2 个答案:

答案 0 :(得分:0)

以下这段代码为我带来了奇迹:

$APPS_SCRIPT_URL = 'https://script.google.com/macros/s/.../exec';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $APPS_SCRIPT_URL); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$content = trim(curl_exec($ch));
curl_close($ch);          

另外,请记住正确授权并发布您的脚本,以便匿名用户(您的PHP脚本)执行

答案 1 :(得分:0)

这是一个使用 POST 请求调用谷歌应用程序脚本的示例

// set some post fields
$post = [
    'username' => 'user1',
    'password' => 'passuser1',
    'gender'   => 1,
];

$url = curl_init('https://script.google.com/macros/s/... your apps script url basically .../exec');
curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
curl_setopt($url, CURLOPT_POSTFIELDS, $post);
curl_setopt($url, CURLOPT_FOLLOWLOCATION, true); //we must follow location -> google apps script makes a small redirect "for security reasons"

// execute!
$response = curl_exec($url);

// close the connection, release resources used
curl_close($url);

// do anything you want with your response
var_dump($response);