使用 PHP 访问 Google 日历 JSON
Access Google Calender JSON using PHP
我从 google api 得到假期很累,这是我的 php 代码。 return json 来自 google 的数据是正确的
$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 压延机 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
}
]
}
**我从具有工作 json 部分
的 js 代码转换了此代码
数据变量是google输出的json,我只需要将这部分转换成php**
for (item in data.items) {
$("#output").append(
"<hr><h3>" + data.items[item].summary + "<h3>" +
"<h4>" + data.items[item].start.date + "<h4>"
);
}
你的代码几乎是正确的,你只是有一个 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>';
}
}
我把$myData
的名字改成了$item
,我觉得这样代码更容易理解,如果你想保留名字[=14=,可以随时更新答案].
我从 google api 得到假期很累,这是我的 php 代码。 return json 来自 google 的数据是正确的
$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 压延机 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
}
]
}
**我从具有工作 json 部分
的 js 代码转换了此代码数据变量是google输出的json,我只需要将这部分转换成php**
for (item in data.items) {
$("#output").append(
"<hr><h3>" + data.items[item].summary + "<h3>" +
"<h4>" + data.items[item].start.date + "<h4>"
);
}
你的代码几乎是正确的,你只是有一个 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>';
}
}
我把$myData
的名字改成了$item
,我觉得这样代码更容易理解,如果你想保留名字[=14=,可以随时更新答案].