Skip to content

Instantly share code, notes, and snippets.

@kaz29
Last active April 8, 2020 22:06
Show Gist options
  • Save kaz29/21c7f10daf1283c37e95ca18a5d8defd to your computer and use it in GitHub Desktop.
Save kaz29/21c7f10daf1283c37e95ca18a5d8defd to your computer and use it in GitHub Desktop.
祭日取得
<?php
use GuzzleHttp\Client;
function getHolidays($holidays_id, $start, $end, $api_key, $num = 50)
{
$url = "https://www.googleapis.com/calendar/v3/calendars/{$holidays_id}/events";
$query_string = "key={$api_key}&timeMin={$start}&timeMax={$end}&maxResults={$num}&orderBy=startTime&singleEvents=true";
$client = new Client();
$response = $client->request(
'GET',
"{$url}?{$query_string}",
[
'timeout' => 120,
'connect_timeout' => 30,
'http_errors' => false,
]
);
if (((int)$response->getStatusCode())/100 != 2) {
return false;
}
$results = json_decode($response->getBody());
$holidays = [];
foreach($results->items as $item)
{
$date = strtotime((string) $item->start->date);
$title = (string)$item->summary;
$origin = null;
if (strpos($title, ' ') !== false) {
list($origin, $title) = explode(' ', $title);
}
$holidays[] = [
'title' => $title,
'origin' => $origin,
'date' => date('Y-m-d', $date),
];
}
return $holidays;
}
$holidays_id = "[email protected]";
$google_api_key = env('GOOGLE_API_KEY');
$holydays = getHolidays($holidays_id, '2020-01-01\T00:00:00\Z', '2020-12-31\T00:00:00\Z', $google_api_key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment