使用PHP访问Google Calender JSON

使用PHP访问Google Calender JSON,php,json,google-calendar-api,Php,Json,Google Calendar Api,我厌倦了从谷歌api获取假期,这里是我的php代码。它正确地从google返回json数据 $calendar_id = urlencode('japanese.ja@holiday.calendar.google.com'); // 取得期間 $start = date("Y-01-01\T00:00:00\Z"); $end = date("Y-12-31\T00:00:00\Z"); $url = 'https://www

我厌倦了从谷歌api获取假期,这里是我的php代码。它正确地从google返回json数据

 $calendar_id = urlencode('japanese.ja@holiday.calendar.google.com');
        // 取得期間
        $start  = date("Y-01-01\T00:00:00\Z");
        $end = date("Y-12-31\T00:00:00\Z");
        $url = 'https://www.googleapis.com/calendar/v3/calendars/en.japanese%23holiday%40group.v.calendar.google.com/events?key=apikeyhere';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        curl_close($ch);
        if (!empty($result)) {
            $json = json_decode($result);

                foreach($json->items as $mydata)

    {       
         foreach($mydata as $values)
         {
              echo $values->summary . "\n";
              echo $values->start. "\n";
         }

    }     
我需要总结一下,从这些数据开始,但上面的代码没有显示任何内容,我厌倦了使用各种foreach,但没有起作用。有人能帮我做一个夏天,然后从这开始,谢谢你

这是来自google calender v3的部分输出json

  {
     "kind": "calendar#events",
     "etag": "\"p33sets73qumdi0g\"",
     "summary": "Holidays in Japan",
     "updated": "2018-02-16T08:53:55.000Z",
     "timeZone": "UTC",
     "accessRole": "reader",
     "defaultReminders": [],
     "nextSyncToken": "CMCd1N-HqtkCEAAYAQ==",
     "items": [
      {
       "kind": "calendar#event",
       "etag": "\"2778543254000000\"",
       "id": "20170109_60o30d9l6go30e1g60o30dr564",
       "status": "confirmed",
       "htmlLink": "https://www.google.com/calendar/event?eid=MjAxNzAxMDlfNjBvMzBkOWw2Z28zMGUxZzYwbzMwZHI1NjQgZW4uamFwYW5lc2UjaG9saWRheUB2",
       "created": "2014-01-09T12:47:07.000Z",
       "updated": "2014-01-09T12:47:07.000Z",
       "summary": "Coming of Age Day",
       "creator": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "organizer": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "start": {
        "date": "2017-01-09"
       },
       "end": {
        "date": "2017-01-10"
       },
       "transparency": "transparent",
       "visibility": "public",
       "iCalUID": "20170109_60o30d9l6go30e1g60o30dr564@google.com",
       "sequence": 0
      },
      {
       "kind": "calendar#event",
       "etag": "\"2778543254000000\"",
       "id": "20170717_60o30d9lcgo30e1g60o30dr564",
       "status": "confirmed",
       "htmlLink": "https://www.google.com/calendar/event?eid=MjAxNzA3MTdfNjBvMzBkOWxjZ28zMGUxZzYwbzMwZHI1NjQgZW4uamFwYW5lc2UjaG9saWRheUB2",
       "created": "2014-01-09T12:47:07.000Z",
       "updated": "2014-01-09T12:47:07.000Z",
       "summary": "Sea Day",
       "creator": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "organizer": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "start": {
        "date": "2017-07-17"
       },
       "end": {
        "date": "2017-07-18"
       },
       "transparency": "transparent",
       "visibility": "public",
       "iCalUID": "20170717_60o30d9lcgo30e1g60o30dr564@google.com",
       "sequence": 0
      },
      {
       "kind": "calendar#event",
       "etag": "\"2778543254000000\"",
       "id": "20170918_60o30d9lcko32e1g60o30dr564",
       "status": "confirmed",
       "htmlLink": "https://www.google.com/calendar/event?eid=MjAxNzA5MThfNjBvMzBkOWxja28zMmUxZzYwbzMwZHI1NjQgZW4uamFwYW5lc2UjaG9saWRheUB2",
       "created": "2014-01-09T12:47:07.000Z",
       "updated": "2014-01-09T12:47:07.000Z",
       "summary": "Respect for the Aged Day",
       "creator": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "organizer": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "start": {
        "date": "2017-09-18"
       },
       "end": {
        "date": "2017-09-19"
       },
       "transparency": "transparent",
       "visibility": "public",
       "iCalUID": "20170918_60o30d9lcko32e1g60o30dr564@google.com",
       "sequence": 0
      },
      {
       "kind": "calendar#event",
       "etag": "\"2778543254000000\"",
       "id": "20171009_60o30d9l6ko30e1g60o30dr564",
       "status": "confirmed",
       "htmlLink": "https://www.google.com/calendar/event?eid=MjAxNzEwMDlfNjBvMzBkOWw2a28zMGUxZzYwbzMwZHI1NjQgZW4uamFwYW5lc2UjaG9saWRheUB2",
       "created": "2014-01-09T12:47:07.000Z",
       "updated": "2014-01-09T12:47:07.000Z",
       "summary": "Sports Day",
       "creator": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "organizer": {
        "email": "en.japanese#holiday@group.v.calendar.google.com",
        "displayName": "Holidays in Japan",
        "self": true
       },
       "start": {
        "date": "2017-10-09"
       },
       "end": {
        "date": "2017-10-10"
       },
       "transparency": "transparent",
       "visibility": "public",
       "iCalUID": "20171009_60o30d9l6ko30e1g60o30dr564@google.com",
       "sequence": 0
      }

     ]
    }
**我将这段代码从js代码转换而来,json代码中包含了可用的json部分

数据变量是从google输出的json,我只需要将这部分转换成php**

for (item in data.items) {
        $("#output").append(
          "<hr><h3>" + data.items[item].summary + "<h3>" +
          "<h4>" + data.items[item].start.date + "<h4>"
        );
      }
for(data.items中的项){
$(“#输出”)。追加(
“
”+数据。项目[项目]。摘要+“”+ “”+数据。项目[项目]。开始日期+“” ); }
你的代码几乎是正确的,你只是有一个
foreach
循环太多,一旦你在项目上迭代,你就可以直接访问它们的属性,不需要循环它们

// Check if we got some results
if (!empty($result)) {

    // Parse the results
    $json = json_decode($result);

    // Iterate over the item property of the resultset
    foreach($json->items as $item) {

        // Each item is an object, 
        // 'summary' and 'start' are properties of 'item'
        // 'date' is a property of 'start'
        echo '<hr><h3>' . $item->summary . '</h3>';

        echo '<h4>' . $item->start->date . '</h4>';

    }
}
//检查我们是否得到了一些结果
如果(!空($result)){
//解析结果
$json=json_decode($result);
//迭代resultset的item属性
foreach($json->items as$item){
//每个项目都是一个对象,
//“摘要”和“开始”是“项目”的属性
//“日期”是“开始”的属性
回显“
”.$item->summary.”; 回显“.$item->start->date.”; } }

我将
$myData
的名称更改为
$item
,我觉得这使代码更容易理解,如果您想保留名称
$myData

请随时更新答案您有APIKEY吗?如果你这样做,$resultsorry密钥用于live site,那么我不能在这里发布的打印\r是什么