LoginSignup
1

More than 5 years have passed since last update.

Google APIs Client Library for PHPでgd:etagを取得する

Last updated at Posted at 2015-11-07

Google SpreadSheetsをGoogle APIs Client Library for PHP 1.1.6で叩くための準備の続き。応用編?

普通に困ったのでメモ。以下はGoogle Spreadsheetを取ってくる動作。

$sheet_id = '#########';
$request = new Google_Http_Request(
    "https://spreadsheets.google.com/feeds/worksheets/{$sheet_id}/private/full?alt=json",
    "GET",
    array(
        "Authorization" => "Bearer {$this->token}",
        "GData-Version" => "3.0"
    )
);

つまりリクエストヘッダにGData-Version: 3.0を足さないと、この場合gd:etagが省略されたJSONが帰ってきてしまう。

↑の結果をjson_decodeするとこうなる;

stdClass Object
(
    [version] => 1.0
    [encoding] => UTF-8
    [feed] => stdClass Object
        (
            [xmlns] => http://www.w3.org/2005/Atom
            [xmlns$openSearch] => http://a9.com/-/spec/opensearch/1.1/
            [xmlns$gs] => http://schemas.google.com/spreadsheets/2006
            [xmlns$gd] => http://schemas.google.com/g/2005
            [gd$etag] => W/"AAAAAAAAAAAAAAAAAAAAAAA."
            [id] => stdClass Object
                (
                    [$t] => https://spreadsheets.google.com/feeds/worksheets/シートID/private/full
                )
...

これで、ターゲットの<link rel="edit">のhref宛に、以下のXMLを投げると、その行だけに上書きできる

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended" gd:etag='"AAAAAAAAAAAAAAAAAAAAAAA."'>
    <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#list"/>
    <id>{$lineid}</id>
    <link rel="self" type="application/atom+xml" href="{$selfhref}"/>
    <link rel="edit" type="application/atom+xml" href="{$edithref}"/>
    <gsx:headname>Update</gsx:use>
</entry>
$request = new Google_Http_Request(
        $edithref,
        "PUT",
        array(
            "Authorization" => "Bearer {$accessToken}",
            "Content-Type" => "application/atom+xml",
            'GData-Version' => "3.0"
        ),
        $postBody // <= ↑のXML
    );

あとはGoogleの公式ドキュメントをよんでがんばって。。

ちなみに最初の問い合わせでシートの一覧をリクエストすると思うが、その時に結果から取得したい値として<link rel="http://schemas.google.com/spreadsheets/2006#listfeed">のhref属性値(つまり$listfeed)があると思うんだけど、
(このURL宛にPUTすると、そのシートのエントリーの中身が取得できる)
そのシート一覧取得時に"GData-Version" => "3.0"つけちゃうと、この要素がいなくなるので注意…。

思った以上に仕様がつまってて、大変すね

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1