如何在PHP中使用cURL发布到Google脚本并返回文本? [英] How do I post to a Google script with cURL in PHP and return text?

查看:128
本文介绍了如何在PHP中使用cURL发布到Google脚本并返回文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力做到最低限度,只是为了让它工作。


这是我的谷歌脚本:

  function doPost(e){
return ContentService.createTextOutput(JSON.stringify(e.parameter));
}

这是我的PHP代码:

  $ url ='https://script.google.com/a/somedomain.com/macros/s/### script id ### / exec'; 
$ data ['name'] =Joe;
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_HTTPHEADER,array(Content-type:multipart / form-data));
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query($ data));
// curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true);
$ result = curl_exec($ ch);
$ error = curl_error($ ch);

执行此操作, $ result 为<$如果我取消注释 CURLOPT_RETURNTRANSFER 行, true

c $ c> $ result =

 < HTML> 
< HEAD>
< TITLE> Bad Request< / TITLE>
< / HEAD>
< BODY BGCOLOR =#FFFFFFTEXT =#000000>
< H1>错误请求< / H1>
< H2>错误400< / H2>
< / BODY>
< / HTML>

$ error 始终为空。



我会使用 doGet(),但我需要发送一些相当大的POST,这将超过GET可以处理的内容。 / p>

如何发布Google脚本并返回数据?
$ b ------ UPDATE - ----

我刚刚了解到我的首席开发人员在前段时间尝试过,并在返回时得出doPost()错误,显然这不仅仅是我。我的看法是Google不够可靠,无法使用。我很想为有人证明我错了。



------ UPDATE 2 - THE FIX ---------



显然这是个问题:

$ $ $ $ $ $ codeurl_setopt($ ch,CURLOPT_POSTFIELDS,http_build_query($数据));

必须是:

  curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data); 

不知道为什么 http_build_query()

解决方案

您需要查看您的HTTP请求标题以查看实际发布的内容。 b
$ b

当遇到问题时,我添加下列选项:

  curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,10 ); 
curl_setopt($ ch,CURLOPT_TIMEOUT,10);
curl_setopt($ ch,CURLOPT_FAILONERROR,true);
curl_setopt($ ch,CURLOPT_ENCODING,);

curl_setopt($ ch,CURLOPT_VERBOSE,true);
curl_setopt($ ch,CURLINFO_HEADER_OUT,true);

CURLINFO_HEADER_OUT 将会添加request_header to curl_getinfo()



你也想看看这些curl_getinfo()元素。


  • request_size

  • size_upload
  • upload_content_length

  • request_header


I'm trying to do the bare minimum, just to get it working.

Here is my Google Script:

function doPost(e) {
  return ContentService.createTextOutput(JSON.stringify(e.parameter));
}

Here is my PHP code:

$url = 'https://script.google.com/a/somedomain.com/macros/s/### script id ###/exec';
$data['name'] = "Joe";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$error  = curl_error($ch);

Executing this, $result is true.

If I uncomment the CURLOPT_RETURNTRANSFER line, $result =

<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Bad Request</H1>
<H2>Error 400</H2>
</BODY>
</HTML>

$error is always empty.

I would use doGet() but I need to send some rather large POSTs that will exceed what GET can handle.

How can I post to a Google script and return data?

------ UPDATE ------

I've just learned my lead developer tried this some time ago and concluded doPost() errors when returning so apparently it's not just me. My take is that Google is simply not reliable enough to use. I would love for someone to prove me wrong.

------ UPDATE 2 - THE FIX ---------

Apparently this was the problem:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

needs to be:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

No idea why http_build_query() caused it to error.

解决方案

You need to look at your HTTP Request header to see what is actually being posted.

When trouble shooting I add these options:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

CURLINFO_HEADER_OUT will add "request_header" to curl_getinfo()

You also want to look at these curl_getinfo() elements.

  • request_size
  • size_upload
  • upload_content_length
  • request_header

这篇关于如何在PHP中使用cURL发布到Google脚本并返回文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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