【问题标题】:Retrieving XML of all Google Calendar Resources on a domain?检索域上所有 Google 日历资源的 XML?
【发布时间】:2015-04-13 16:28:03
【问题描述】:

我目前正在使用 PHP 编写一个表格,该表格将合并两个不同的任务:为活动预订空间,以及为在线日历注册活动。我已经用 PHP 的 API 客户端确定了 Google 日历,但 Calendar Resources 仍然被证明是一个问题。

以下是关于我目前所在位置的一些信息:

我目前的困境涉及我从调用中返回的数据。当我使用 Google OAuth2 Playground 使用上述 URI 发出请求时,它会毫无问题地检索 XML 数据。当我在 PHP 中进行同样的尝试时,我得到了具有以下格式的单个字符串中的数据:

我知道每个请求每个请求只会返回 100 个资源,并且操场和我的 php 都返回似乎是 100 个条目,所以它似乎不一定是授权问题。话虽这么说,但我不确定我会做哪些与操场不同的事情。

我的 cURL 请求如下(检索到 access_token 后):

$url = 'https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/';

$token = json_decode($_SESSION['access_token'], true);
$access_token = $token['access_token'];
$token_type = $token['token_type'];

//open connection
$ch = curl_init();

curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => array('Authorization: '.$token_type.' '.$access_token,),
));

// $output contains the returned data
$output = curl_exec($ch);

if($output === false) { echo 'Curl error: ' . curl_error($ch); }
else { echo $output; }

// close curl resource to free up system resources
curl_close($ch);   

我是否缺少某些东西,cURL 设置或标题?我猜不出它会是什么,尤其是当 Playground 的请求只是以下内容时:

GET /a/feeds/calendar/resource/2.0/domain/ HTTP/1.1
Host: apps-apis.google.com
Content-length: 0
Authorization: Bearer access_token

更新

Hans Z. 的回答促使我(谢谢!)寻找将 XML 数据放入数组的方法,因为我需要使用获得的数据而不是显示它。 (我很抱歉最初没有说清楚。)There are a lot of posts on the subject,在通过 SimpleXML 输出输出后,我检索以下内容作为每个资源 (x100) 的条目:

[0] => SimpleXMLElement Object
(
    [id] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
    [updated] => 2015-02-18T17:04:16.481Z
    [link] => Array
    (
        [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
            (
                [rel] => self
                [type] => application/atom+xml
                [href] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
            )
        )

        [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
            (
                [rel] => edit
                [type] => header('Content-type: application/xml');
                [href] => https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621
            )
        )
    )
)

如上所示,这不提供有关资源本身的任何信息,例如它的名称或电子邮件(或 id,以任何直接无解析方式)。我尝试通过 SimpleXML 从操场运行 XML 输出,并且正在恢复完全相同的输出。假设它与 SimpleXML 相关,我进行了更多搜索,发现了 this questionthis questionthis blog post。我自己一直在研究他们的建议,但到目前为止收效甚微。如果有人想在我之前弄清楚(因为我必须在今天剩下的大部分时间里参加会议),这里是 XML 结构的示例:

<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:apps='http://schemas.google.com/apps/2006'>
    <id>https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain</id>
    <updated>2015-02-18T17:03:03.057Z</updated>
    <link rel='next' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/?start=579841149621'/>
    <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
    <link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
    <link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain'/>
    <openSearch:startIndex>1</openSearch:startIndex>

    <entry>
        <id>https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621</id>
        <updated>2015-02-18T17:03:03.056Z</updated>
        <link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621'/>
        <link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/calendar/resource/2.0/domain/579841149621'/>

        <apps:property name='resourceId' value='579841149621'/>
        <apps:property name='resourceCommonName' value='Projector'/>
        <apps:property name='resourceEmail' value='domain_key@resource.calendar.google.com'/>
    </entry>

    //99 more times:
    <entry>
        ...
    </entry>

【问题讨论】:

  • 在 php github.com/google/google-api-php-client 上查看此链接以获取 oauth 客户端
  • 你给token_type作为“Bearer”吗??
  • @SGC $token_type 是从使用 PHP 客户端获得的令牌中提取的 - $_SESSION['access_token']- 其值为 Bearer。这将传递给 cURL 调用。
  • @SGC 更正:$_SESSION['access_token'] 是令牌本身,$_SESSION['token_type'] 的值为“Bearer”。
  • @NickMischler:您的代码表明$_SESSION['access_token'] 是一个包含实际访问令牌和令牌类型的JSON 对象;是哪个?

标签: php xml curl google-admin-sdk


【解决方案1】:

对于那些宁愿将资源作为数组处理的人,以下是 Hans Z. 给出的解决方案的替代方案:

$array = json_decode($output, true);
$resources = $array['feed']['entry'];

foreach ($resources as $resource) {
    $id = $resource['apps$property']['0']['value'];
    $name = $resource['apps$property']['1']['value'];
    $email = $resource['apps$property']['2']['value'];

    //do something with data
}

【讨论】:

    【解决方案2】:

    您的脚本运行良好,但您正在浏览器中查看脚本的输出。浏览器将尝试将 XML 输出解析为 HTML,从而得到观察到的结果。更改您的代码,使其向浏览器指示响应是 XML,如下所示:

    header('Content-type: application/xml');
    if($output === false) { echo 'Curl error: ' . curl_error($ch); }
    else { echo $output; }
    

    或使用命令行 PHP 解释器查看输出。

    编辑:

    您可能更乐意在 PHP 中解析 JSON;您可以通过在请求中添加 alt=json 来获取 JSON 格式的结果,如下所示:

    https://apps-apis.google.com/a/feeds/calendar/resource/2.0/<domain>/?alt=json
    

    并将$output = json_decode($output) 应用于输出;然后您可以访问所需的数据,例如第一个资源的邮箱地址,如下:

    $output->feed->entry[0]->{'apps$property'}[2]->value 
    

    【讨论】:

    • 感谢 Hans Z。我没有提到我正在寻找和处理这些数据。我尝试了您的建议并收到了 XML 解析错误,因为该页面总体上应该是 HTML,但您的回答确实促使我根据我的需要进行更多研究。如果您有兴趣,我已经用其他信息更新了问题。
    • 添加了 JSON 解析示例
    • 谢谢。我已经回答了我更愿意亲自使用的替代方案,并提出了修改以提高示例的可读性,但您的答案正是我需要找到的。
    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 2011-11-07
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    相关资源
    最近更新 更多