Php Google电子表格API如何插入新行

Php Google电子表格API如何插入新行,php,google-api,google-sheets,Php,Google Api,Google Sheets,到目前为止,我只使用API从Google电子表格中检索数据,但我不知道如何插入数据。我看了所有的例子,但没有一个是足够清楚的 为了检索数据,我所要做的就是构造一个URL并在PHP中使用CURL检索它,如下所示: //Get spreadsheet data $headers = array( "Authorization: GoogleLogin auth=" . $auth, "GData-Version: 3.0", );

到目前为止,我只使用API从Google电子表格中检索数据,但我不知道如何插入数据。我看了所有的例子,但没有一个是足够清楚的

为了检索数据,我所要做的就是构造一个URL并在PHP中使用CURL检索它,如下所示:

    //Get spreadsheet data
    $headers = array(
        "Authorization: GoogleLogin auth=" . $auth,
        "GData-Version: 3.0",
        );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/tq?tqx=out:html&tq=select%20D%2C%20E%20where%20B%3D&key=1c1xxxxxxxxxxxxx                                                                                                                                           
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1
    $response= curl_exec($curl);

,如何构造要插入的类似URL?我认为会有一个类似的网址,可以使用。有人能给我指一下正确的方向吗?我在这里找不到关于它的信息

嗯,没有答案,但更多的研究指出,在“添加列表行”的“协议”链接下单击,可以为我提供足够的线索来构建以下内容:

//create xml with each element representing a column header (note: for some reason the headers must only contain alphanumeric characters)
$xml = "
<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>
  <gsx:id>123</gsx:id>
  <gsx:status>123</gsx:status>
  <gsx:date>4/26/2014</gsx:date>
  <gsx:user>bob</gsx:user>
</entry>";

//set headers which includes auth from elsewhere in the code
$headers = array(
    "Authorization: GoogleLogin auth=" . $auth,
    "GData-Version: 3.0",
    "Content-Type: application/atom+xml",
    );

//post the xml. The key in the url is taken from the actual url when you view the sheet 
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/xxxxxxxxxxxxxxxkeyxxxxxxxxx/0/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
//创建xml,每个元素表示一个列标题(注意:由于某些原因,标题只能包含字母数字字符)
$xml=”
123
123
4/26/2014
上下快速移动
";
//设置包含代码中其他位置的身份验证的标题
$headers=数组(
“授权:GoogleLogin auth=”.$auth,
“GData版本:3.0”,
“内容类型:应用程序/atom+xml”,
);
//发布xml。url中的键取自查看工作表时的实际url
$curl=curl_init();
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_URL,”https://spreadsheets.google.com/feeds/list/xxxxxxxxxxxxxxxkeyxxxxxxxxx/0/private/full");
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl,CURLOPT_POSTFIELDS,“$xml”);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$response=curl\u exec($curl);

希望这对其他人有所帮助

以下是我在授权过程中做的一些笔记。我认为谷歌的文档很薄。希望这能帮助别人

自2017年起,身份验证不再如本文所述有效。

查看此帖子-比“官方”文档更容易访问:

通过创建新的电子表格

通过创建新的应用程序特定密码

使用特定于应用程序的密码创建身份验证令牌:
curl-vhttps://www.google.com/accounts/ClientLogin --数据编码电子邮件=yourname@gmail.com--数据urlencode Passwd=-d accountType=GOOGLE-d service=wise

不必是GMail地址——使用你的谷歌账户地址

我还使用了
-d source=…
(如上面的“using curl”文档中所示),但我想这是不必要的

至于
wise
字符串,请参阅以获取服务名称列表

复制服务器返回的文档中的身份验证密钥:
auth=…

如果您能以某种方式使身份验证工作正常(我没有),那么其余的可能仍然有效:

获取您的电子表格列表:
curl-v-H'授权:GoogleLogin auth=…'https://spreadsheets.google.com/feeds/spreadsheets/private/full >电子表格.xml

查找电子表格的工作表URL:
xmllint--format spreadsheets.xml | less

查找电子表格的名称,从

此处解释:,但使用不同的rel URI:-(

获取电子表格中的工作表列表:
curl-v-H'授权:GoogleLogin auth=…'https://spreadsheets.google.com/feeds/worksheets/.../private/full >worksheets.xml

查找工作表的listfeed URL:
xmllint——这里应该描述format workheets.xml | less
,但是,URI与我看到的不匹配…
看起来不错

最后,添加行。这是user2029890在其回答中描述的部分:
curl-v-H'内容类型:application/atom+xml'-H'授权:GoogleLogin auth=…'-d'123bob'https://spreadsheets.google.com/feeds/list/.../.../private/full

XML元素名称,例如
gsx:id
gsx:user
,必须与列标题匹配


XML解析器非常挑剔-它需要在属性值周围加上双引号。

我试图按curl插入行。我的电子表格链接如下:那么这个电子表格的键是什么?我得到的请求URI无效!!如果我没有弄错$api=>“acces token”或凭据。你能清除一次吗?谢谢!确实帮了大忙。我猜是的您应该在末尾添加
curl\u close($curl);
。@AwladLiton您需要找到工作表的listfeed URL。请参阅我的答案。我收到了
400个错误请求
响应:无法写入空行;请改用delete`请帮助