Google カレンダー API v3 から祝祭日を取得するサンプル
v2は今はサポートを終了して使えませんってことで
v2の場合 こんな感じでした。
<?php // Google APIより祝日取得 $holidays_url = sprintf( 'http://www.google.com/calendar/feeds/%s/public/full-noattendees?start-min=%s&start-max=%s&max-results=%d&alt=json' , 'outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com' , // 'japanese@holiday.calendar.google.com' , date("Y").'-01-01' , // 取得開始日 (date("Y")+1).'-12-31' , // 取得終了日 50 // 最大取得数 ); if ( $results = file_get_contents($holidays_url) ) { $results = json_decode($results, true); $holidays = array(); foreach ($results['feed']['entry'] as $val ) { $date = $val['gd$when'][0]['startTime']; $title = $val['title']['$t']; $holidays[$date] = $title; } ksort($holidays); }
v3の場合
うまく行かなかった方法
ソース引用元 [PHP] Google Calendar API v3 で日本の祝日を取得する | memo.dogmap.jp
APIキーがいるみたいなのでgoogleアカウントが必要です。
取り方
Google APIキー取得方法 - purazumakoiの[はてなブログ]
実際の設置コード
<?php // 月初日 $first_day = mktime(0, 0, 0, intval($month), 1, intval($year)); // 月末日 $last_day = strtotime('-1 day', mktime(0, 0, 0, intval($month) + 1, 1, intval($year))); $api_key = '取得したAPIキー'; $holidays_id = 'outid3el0qkcrsuf89fltf7a4qbacgt9@import.calendar.google.com'; // mozilla.org版 //$holidays_id = 'japanese__ja@holiday.calendar.google.com'; // Google 公式版日本語 //$holidays_id = 'japanese@holiday.calendar.google.com'; // Google 公式版英語 $holidays_url = sprintf( 'https://www.googleapis.com/calendar/v3/calendars/%s/events?'. 'key=%s&timeMin=%s&timeMax=%s&maxResults=%d&orderBy=startTime&singleEvents=true', $holidays_id, $api_key, date('Y-m-d', $first_day).'T00:00:00Z' , // 取得開始日 date('Y-m-d', $last_day).'T00:00:00Z' , // 取得終了日 31 // 最大取得数 ); if ( $results = file_get_contents($holidays_url) ) { $results = json_decode($results); $holidays = array(); foreach ($results->items as $item ) { $date = strtotime((string) $item->start->date); $title = (string) $item->summary; $holidays[date('Y-m-d', $date)] = $title; } ksort($holidays); }
うまく行った方法
PHPで日本の祝日のJSONをGoogleカレンダーから取得する(API認証等不要) - きじとら
↑のようにcurlでやればよいが、httpsでやるとエラーが出る場合があるのでこうした
$url = //www.google.com/calendar/feeds/{$calendar_id}/public/basic?start-min={$start}&start-max={$end}&max-results=30&alt=json";
このやり方だと上手くいくがcURLがないといけない。