Google API - 使用PUT和PHP和Curl更新电子表格

时间:2014-05-13 14:31:46

标签: php curl google-api google-spreadsheet-api

我正在使用来自https://developers.google.com/google-apps/spreadsheets/

的Google自己的示例

他们说:

要更新现有行的内容,首先检索要更新的行,根据需要进行修改,然后将PUT请求(包含邮件正文中更新的行)发送到行的编辑URL。

确保PUT条目中的id值与现有条目的id完全匹配。编辑URL在以下行条目中突出显示:

<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>

我一直在尝试使用以下CURL发送此信息(使用我自己的数据,但结构完全相同):

$headers = array(
            "Authorization: GoogleLogin auth=" . $google_auth,
            "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/$row_id");
        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格式错误,这很奇怪,因为它来自Google的示例,当我从自己的电子表格中检索列表供稿时,我得到相同的响应。我如何更正,还有其他问题吗?

当我将此xml粘贴到http://www.freeformatter.com/xml-formatter.html时,我收到错误

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.

感谢。

3 个答案:

答案 0 :(得分:1)

嗯,没有真正的回应....但我终于得到了它的工作。希望这有助于其他人。这个想法是你需要使用通过feed检索的命名空间。

所以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="&quot;S0wCTlpIIip7ImA0X0QI&quot;">
   <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>

CURL应该如下:

$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;

答案 1 :(得分:0)

如果您想要检索和更新正确的自定义XML而不是通用的Atom XML文档,那么您可以尝试APISpark PaaS,它具有可以创建和托管自定义Web API的GSpreadsheets包装。

请参阅教程:https://apispark.com/docs/tutorials/google-spreadsheet

答案 2 :(得分:0)

这应该对你有帮助

$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);