GoogleAPI-使用PUT和PHP和Curl更新电子表格
我用的是谷歌自己的例子 他们说: 要更新现有行的内容,请首先检索要更新的行,根据需要对其进行修改,然后将PUT请求(更新的行位于消息正文中)发送到该行的编辑URL 确保您放置的条目中的id值与现有条目的id完全匹配。编辑URL在以下行条目中高亮显示:GoogleAPI-使用PUT和PHP和Curl更新电子表格,php,curl,google-api,google-spreadsheet-api,Php,Curl,Google Api,Google Spreadsheet Api,我用的是谷歌自己的例子 他们说: 要更新现有行的内容,请首先检索要更新的行,根据需要对其进行修改,然后将PUT请求(更新的行位于消息正文中)发送到该行的编辑URL 确保您放置的条目中的id值与现有条目的id完全匹配。编辑URL在以下行条目中高亮显示: <entry gd:etag='"S0wCTlpIIip7ImA0X0QI"'> <id>https://spreadsheets.google.com/feeds/list/key/worksheetId/privat
<entry gd:etag='"S0wCTlpIIip7ImA0X0QI"'>
<id>https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId</id>
<updated>2006-11-17T18:23:45.173Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006"
term="http://schemas.google.com/spreadsheets/2006#list"/>
<title type="text">Bingley</title>
<content type="text">Hours: 10, Items: 2, IPM: 0.0033</content>
<link rel="self" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId"/>
<link rel="edit" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId/version"/>
<gsx:name>Bingley</gsx:name>
<gsx:hours>20</gsx:hours>
<gsx:items>4</gsx:items>
<gsx:ipm>0.0033</gsx:ipm>
</entry>
XML似乎格式不正确,这很奇怪,因为它来自Google的示例,我从自己的电子表格检索列表提要时得到了相同的响应。我如何更正,是否存在其他问题
当我将此xml粘贴到中时,我收到一个错误
Unable to parse any XML input. org.jdom2.input.JDOMParseException: Error on line 1: The prefix "gd" for attribute "gd:etag" associated with an element type "entry" is not bound.
谢谢 嗯,没有真正的回应……但我终于让它起作用了。希望这能帮助其他人。想法是您需要使用提要检索的名称空间 因此,XML应该如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" gd:etag=""S0wCTlpIIip7ImA0X0QI"">
<id>https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId</id>
<updated>2006-11-17T18:23:45.173Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#list" />
<title type="text">Bingley</title>
<content type="text">Hours: 10, Items: 2, IPM: 0.0033</content>
<link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId" />
<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId/version" />
<gsx:name>Bingley</gsx:name>
<gsx:hours>20</gsx:hours>
<gsx:items>4</gsx:items>
<gsx:ipm>0.0033</gsx:ipm>
</entry>
$headers = array(
"Authorization: GoogleLogin auth=" . $google_auth, //google_auth retrieved earlier
"GData-Version: 3.0",
"Content-Type: application/atom+xml",
"If-Match: *",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/$feed/0/private/full/$id"); //feed is obtained from spreadsheet url and id can be obtained by retrieving list feed
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
echo $response;
如果希望检索和更新适当的自定义XML而不是通用的Atom XML文档,那么可以尝试APISpark PaaS,它有一个GSpreadsheets包装器,可以创建和托管自定义web API
请参阅教程:这将对您有所帮助
$accessToken = $ACCESS_TOKEN;
$key = $SPREADSHEET_ID;
$sheetId = $SHEET_ID;
$editUrl = "https://spreadsheets.google.com/feeds/cells/$key/$sheetId/private/full/R2C4?access_token=$accessToken";
$entry = '<?xml version="1.0" encoding="UTF-8" ?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006"><id>https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4</id><link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4"/><gs:cell row="2" col="4" inputValue="Enter_Value_Here">Enter_Value_Here</gs:cell></entry>';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $editUrl );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml','If-Match: *','Content-length: ' . strlen($entry)));
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $entry );
$content = curl_exec($ch);
if($content === false)
{
echo 'Curl error: ' . curl_error($ch)."<br>";
}
else
{
echo 'Operation completed without any errors <br>';
$information = curl_getinfo($ch);
}
curl_close($ch);
$accessToken=$ACCESS\u TOKEN;
$key=$SPREADSHEET\u ID;
$sheetId=$SHEET\u ID;
$editUrl=”https://spreadsheets.google.com/feeds/cells/$key/$sheetId/private/full/R2C4?访问令牌=$accessToken”;
$entry=https://spreadsheets.google.com/feeds/cells/“.$key./”.$sheetId./private/full/r2c4在此处输入值”;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$editUrl);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“PUT”);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/atom+xml','If-Match:','Content-length:'.strlen($entry));
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$entry);
$content=curl\u exec($ch);
如果($content==false)
{
回显“卷曲错误:”。卷曲错误($ch)。“
”;
}
其他的
{
echo“操作已完成,无任何错误
”;
$information=curl\u getinfo($ch);
}
卷曲关闭($ch);
这看起来确实很有趣,尽管快速浏览只会提到检索或添加新数据。我确信其中有一些关于更新和删除的内容。是的,如果您查看教程末尾创建的Web API,它有HTTP PUT(用于更新)和DELETE方法。但是为什么要在标记中插入时间戳呢?这不应该是谷歌服务器的响应吗?关于请求中需要包含的xmlns属性,您是对的。
$accessToken = $ACCESS_TOKEN;
$key = $SPREADSHEET_ID;
$sheetId = $SHEET_ID;
$editUrl = "https://spreadsheets.google.com/feeds/cells/$key/$sheetId/private/full/R2C4?access_token=$accessToken";
$entry = '<?xml version="1.0" encoding="UTF-8" ?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006"><id>https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4</id><link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4"/><gs:cell row="2" col="4" inputValue="Enter_Value_Here">Enter_Value_Here</gs:cell></entry>';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $editUrl );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml','If-Match: *','Content-length: ' . strlen($entry)));
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $entry );
$content = curl_exec($ch);
if($content === false)
{
echo 'Curl error: ' . curl_error($ch)."<br>";
}
else
{
echo 'Operation completed without any errors <br>';
$information = curl_getinfo($ch);
}
curl_close($ch);