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>";
        ?>
    
    打赏 评论
  • dss524049 2013-09-05 10:06

    Try decoding the json as array not object

    json_decode(file_get_contents("url"), true);
    
    打赏 评论

相关推荐 更多相似问题