正在检索域上所有Google日历资源的XML


Retrieving XML of all Google Calendar Resources on a domain?

我目前正在用PHP编写一个表单,该表单将合并两个不同的任务:为活动预订空间和为在线日历注册活动。我已经用API Client for PHP确定了Google日历部分,但日历资源仍然是一个问题。

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

  • 我正在使用常规的API来处理Calendar和Resource作用域上的OAuth2授权。
    • 资源范围:https://apps-apis.google.com/a/feeds/calendar/resource/
  • 我使用访问令牌向Resource提要发出cURL GET请求,试图检索所有可用的资源。
    • 资源调用:https://apps-apis.google.com/a/feeds/calendar/resource/2.0//
  • 假设以上操作有效,我将使用从XML中提取的资源电子邮件来邀请选定的资源参加使用日历API制作的活动

我目前的困境涉及我从通话中返回的数据。当我使用Google OAuth2 Playground用上面的URI发出请求时,它检索XML数据时没有问题。当我在PHP中进行同样的尝试时,我会在一个字符串中获得以下格式的数据:

  • https://apps-apis.google.com/a/feeds/calendar/resource/2.0//5798411496212015-02-12T18:42:23.268Z
  • "request_uri"。"resource_id"。"访问日期"(x 100)

我知道每个请求每个请求只返回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设置或标头,我丢失了?我猜不出会是什么,尤其是当游乐场的请求如下时:

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数据放入数组的方法,因为我需要使用获得的数据,而不是显示它

[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的东西,我做了更多的搜索,发现了这个问题,这个问题,还有这篇博客文章。我自己也一直在尝试他们的建议,但到目前为止收效甚微。如果有人想在我之前弄清楚(因为我必须在今天剩下的大部分时间里参加会议),这里有一个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>

您的脚本运行良好,但您正在浏览器中查看脚本的输出。浏览器将尝试将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

并将CCD_ 2应用于输出;然后,您可以访问所需的数据,例如第一个资源的电子邮件地址,如下所示:

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

以下是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
}