google calendar api を使って、オリジナルのカレンダーを作りました。
忘れてしまわないように、メモしておきます。
作業内容
google api consoleでプロジェクト作成
APIキーを作成。
サンプルとして、APIキーの利用制限は無しにします。
composerでgoogleのapiセット?をインストール。
composer require google/apiclient:^2.0
参考: https://github.com/google/google-api-php-client
googleカレンダーを作成。
「+」ボタンを押して「新しいカレンダー」をクリック。
設定と共有から、「アクセス権限」の欄のチェックボックス「一般公開して誰でも利用できるようにする」にチェック。
「カレンダーの統合」にあるカレンダーIDをメモ。
const APPLICATION_NAME = 'プロジェクトの名前をコピペ';
const DEVELOPER_KEY = 'APIキーをコピペ';
const CALENDAR_ID = 'メモしたカレンダーIDをコピペ';
public $service;
/*
* constructor
*/
public function __construct() {
date_default_timezone_set('Asia/Tokyo');
require_once '/path/to/your-project/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName(self::APPLICATION_NAME);
$client->setDeveloperKey(self::DEVELOPER_KEY);
$this->service = new Google_Service_Calendar($client);
}
/*
* カレンダーのイベントを取得
*/
public function getCalEvents() {
$now = date('c');
$opt_params = [
'maxResults' => 300,
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeZone' => 'Asia/Tokyo',
'timeMin' => $now ,
'timeMax' => date('Y-m-d' ,strtotime("+6 day" ,strtotime(substr($now, 0, 10)))) . 'T23:59:59+09:00',// サンプルとして1週間分取得
];
$results = $this->service->events->listEvents(self::CALENDAR_ID, $opt_params)->items;
$rtn = '';
foreach ($results as $value) {
$rtn.= '欲しいデータhtml';
}
return $rtn;
}
おしまい