从Google Calendar返回的JSON转换数据时,我的PHP哪里出错了


Where am I going wrong in my PHP when converting data from this JSON returned from Google Calendar?

我有一个简单的PHP脚本,使用Google API从Google日历中获取JSON数据。目标是遍历JSON,然后对于每个事件,只返回开始日期和摘要。

这是我的代码:

$eventsCalendar = json_decode(file_get_contents("https://www.googleapis.com/calendar/v3/calendars/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxgroup.calendar.google.com/events?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
//var_dump($eventsCalendar);
$outputJSON = array();
foreach ($eventsCalendar as $event)
{
 array_push($outputJSON, array(
    'title' => $event['summary'],
    'start' => $event['start']['dateTime'],
  ));
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($outputJSON );

我得到的是看起来像这样的废话:

[{"title":"c","start":"c"},{"title":"'"","start":"'""},{"title":"T","start":"T"},{"title":"","start":""},{"title":"2","start":"2"},{"title":"A","start":"A"},{"title":"r","start":"r"},{"title":null,"start":null}]

以下是var_dump($eventsCalendar);:的部分结果

array(8) { ["kind"]=> string(15) "calendar#events" ["etag"]=> string(57) ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"" ["summary"]=> string(24) "xxxxxxxxxxxxxxxxxxxxxxxxx" ["description"]=> string(0) "" ["updated"]=> string(24) "2013-09-05T10:12:10.027Z" ["timeZone"]=> string(10) "xxxxx/xxxxx" ["accessRole"]=> string(6) "reader" ["items"]=> array(7) { [0]=> array(17) { ["kind"]=> string(14) "calendar#event" ["etag"]=> string(52) ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"" ["id"]=> string(26) "xxxxxxxxxxxxxxxxxxxxx" ["status"]=> string(9) "confirmed" ["htmlLink"]=> string(138) "https://www.google.com/calendar/event?xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ["created"]=> string(24) "2013-08-27T00:44:24.000Z" ["updated"]=> string(24) "2013-08-29T05:33:04.171Z" ["summary"]=> string(18) "xxxxxxxxxxxxxxxxxxxxx" ["location"]=> string(27) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ["creator"]=> array(2) { ["email"]=> string(34) "xxxxxxxxxxxxxxxxxxxxxx@gmail.com" ["displayName"]=> string(20) "xxxxxxxxxxxxxxxxxxxxxxxxx" } ["organizer"]=> array(3) { ["email"]=> string(52) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com" ["displayName"]=> string(24) "xxxxxxxxxxxxxxxxxxxxxxxxxx" ["self"]=> bool(true) } ["start"]=> array(2) { ["dateTime"]=> string(25) "2013-09-06T20:00:00+09:00" ["timeZone"]=> string(10) "xxxxx/xxxxx" } ["end"]=> array(2) { ["dateTime"]=> string(25) "2013-09-03T21:00:00+09:00" ["timeZone"]=> string(10) "xxxxx/xxxxx" } ["recurrence"]=> array(1) { [0]=> string(28) "RRULE:FREQ=MONTHLY;BYDAY=1TU" } ["visibility"]=> string(6) "public" ["iCalUID"]=> string(37) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx@google.com" ["sequence"]=> int(1) } [1]=> array(16) { ["kind"]=> string(14) "calendar#event" ["etag"]=> string(52) ""xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"" ["id"]=> string(26) "xxxxxxxxxxxxxxxxxxxxxxxx" ["status"]=> string(9) "confirmed" ["htmlLink"]=> string(138) "https://www.google.com/calendar/event?eid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ["created"]=> string(24) "2013-08-25T08:03:53.000Z" ["updated"]=> string(24) "2013-08-29T05:33:15.417Z" ["summary"]=> string(18) "xxxxxxxxxxxxxxxxxxxxxxxxxxx" ["location"]=> string(27) "xxxxxxxxxxxxxxxxxxxxxxxxxxx" ["creator"]=> array(2) { ["email"]=> string(34) "xxxxxxxxxxxxxxxxxxxxx@gmail.com" ["displayName"]=> string(20) "xxxxxxxxxxxxxxxxxxxxxx" } ["organizer"]=> array(3) { ["email"]=> string(52) "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com" ["displayName"]=> string(24) "xxxxxxxxxxxxxxxxxxxxxxxx" ["self"]=> bool(true) } ["start"]=> array(2) { ["dateTime"]=> string(25) "2013-09-17T20:00:00+09:00" ["timeZone"]=> string(10) "xxxxxx/xxxxxxxxxx" } ["end"]=> array(2) { ["dateTime"]=> string(25) "2013-09-18T21:00:00+09:00" ["timeZone"]=> string(10) "xxxxxxxx/xxxxxxxxxxxxx" } 

以下是print_r($eventsCalendar):的结果

Array ( [kind] => calendar#events [etag] => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" [summary] => xxxxxxxxxxxxxxxxxxxxxxxx [description] => [updated] => 2013-09-06T10:35:34.449Z [timeZone] => xxxx/xxxxx [accessRole] => reader [items] => Array ( [0] => Array ( [kind] => calendar#event [etag] => "xxxxxxxxxxxxxxxxxxxxxxxxxx" [id] => xxxxxxxxxxxxxxxxxxxxxxxxxxxx [status] => confirmed [htmlLink] => https://www.google.com/calendar/event?eid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [created] => 2013-08-24T00:44:24.000Z [updated] => 2013-08-29T05:33:04.171Z [summary] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [location] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [creator] => Array ( [email] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@gmail.com [displayName] => xxxxxxxxxxxxxxxxxxxxxxxxxxxx ) [organizer] => Array ( [email] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com [displayName] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [self] => 1 ) [start] => Array ( [dateTime] => 2013-09-03T20:00:00+09:00 [timeZone] => xxxxxx/xxxxxxxx ) [end] => Array ( [dateTime] => 2013-09-03T21:00:00+09:00 [timeZone] => xxx/xxx ) [recurrence] => Array ( [0] => RRULE:FREQ=MONTHLY;BYDAY=1TU ) [visibility] => public [iCalUID] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@google.com [sequence] => 1 ) [1] => Array ( [kind] => calendar#event [etag] => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" [id] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxx [status] => confirmed [htmlLink] => https://www.google.com/calendar/event?eid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [created] => 2013-08-25T08:03:53.000Z [updated] => 2013-08-29T05:33:15.417Z [summary] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [location] => xxxxxxxxxxxxxxxxxxxxxxxxxxxx [creator] => Array ( [email] => xxxxxxxxxxxxxxxxxxxxxx@gmail.com [displayName] => xxxxxxxxxxxxxxxxxxxx) [organizer] => Array ( [email] => xxxxxxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com [displayName] => xxxxxxxxxxxxxxxxxxx [self] => 1 ) [start] => Array ( [dateTime] => 2013-09-17T20:00:00+09:00 [timeZone] => xxxx/Txxxxxxxx ) [end] => Array ( [dateTime] => 2013-09-17T21:00:00+09:00 [timeZone] => xxxx/xxx ) [recurrence] => Array ( [0] => RRULE:FREQ=MONTHLY;BYDAY=3TU ) [iCalUID] => xxxxxxxxxxxxxxxxxxxxx@google.com [sequence] => 0 ) [2] => Array ( [kind] => calendar#event [etag] => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" [id] => xxxxxxxxxxxxxxxxxxxxxxxxxxx [status] => confirmed [htmlLink] => https://www.google.com/calendar

我很确定这是因为我不清楚我从谷歌得到的JSON的结构,以及当它转换成PHP数组时是如何表示的。

我的代码哪里出了问题,我如何将从谷歌获得的JSON剥离为我想要的信息?

试试这个

<?php
    foreach ($eventsCalendar['items'] as $event){
        $summary[] = $event['summary'];
        $datetime[]  = $event['start']['dateTime'];     
        }
    echo "<pre>";
    print_r($summary);
    echo "</pre>---------------------------------------";
    echo "<pre>Date Time ----------------------------------------------------";
    print_r($datetime);
    echo "</pre>";
    ?>

尝试将json解码为数组而非对象

json_decode(file_get_contents("url"), true);