dotelauv682684 2013-09-05 10:01
浏览 49
已采纳

从谷歌日历返回的这个JSON转换数据时,我在PHP中出错了什么地方?

I have a simple PHP script where I get JSON data from a Google Calendar using the Google API. The goal is to go through the JSON and then for each event, return only the start date and summary.

This is my code:

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

What I'm getting back is nonsense that looks like this:

[{"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}]

Here is a section of the results from 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" } 

Here are the results of 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

I'm pretty sure it's because I'm not clear on the structure of the JSON I'm getting from Google, and how it's represented when it's converted into a PHP array.

Where have I gone wrong in my code, and how do I strip the JSON I get from Google down into just the information I want?

  • 写回答

2条回答 默认 最新

  • douzhixun8393 2013-09-05 10:50
    关注

    Try this

    <?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>";
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决