我从谷歌api获得假期很累,这是我的PHP代码。从google corretcly返回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,我只需将此部分转换为php **
for (item in data.items) {
$("#output").append(
"<hr><h3>" + data.items[item].summary + "<h3>" +
"<h4>" + data.items[item].start.date + "<h4>"
);
}
答案 0 :(得分:1)
您的代码几乎是正确的,您只有一个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
,我觉得它使代码更容易理解,如果您想保留名称$mydata
,请随时更新答案。