乙です。
さて、今回はPHPでgoogleカレンダーへのデータ更新をします。
とりあえず、google-api-php-client-x.x.xを使用します。適当に設置してください。
あと、もろもろgoogleのapiを使用するためのことをしておいてください。
さてと・・・
phpソースをさらします・・
hoge.php
<?php
// カレンダーのIDは必須なので、無い場合は終了させる
if (!isset($_REQUEST['calendar_id']) && !$_REQUEST['calendar_id']) {
exit;
}
$calendar_id = $_REQUEST['calendar_id'];
$calendar_title = isset($_REQUEST['calendar_title'])?$_REQUEST['calendar_title']:"バイト";
$day = (isset($_REQUEST['day']) && count($_REQUEST['day']))?$_REQUEST['day']:"";
$min_d = isset($_REQUEST['min_d'])?$_REQUEST['min_d']:"";
$max_d = isset($_REQUEST['max_d'])?$_REQUEST['max_d']:"";
if (!$max_d || !$min_d) {
exit;
}
require_once("./google-api-php-client-x.x.x/vendor/autoload.php");
$json_path = __DIR__.'/json/「googleAPIのプロジェクト名」-[なんか適当な文字列の羅列].json';
$client = new Google_Client();
$client->setAuthConfig($json_path);
$client->setApplicationName("Google Calendar PHP API");
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAccessType('offline');
$client->setApprovalPrompt ("force");
$service = new Google_Service_Calendar($client);
//該当期間のカレンダーデータを取得
$optParams = array(
'orderBy' => 'startTime',
'singleEvents' => TRUE,
'timeMin' => date('c', strtotime($min_d)),
'timeMax' => date('c', strtotime($max_d . " +1 day")),
);
// 取得実行
$results = $service->events->listEvents($calendar_id, $optParams);
// GoogleカレンダースケジュールのデータとPOSTされたデータを比較します
foreach ($results->items as $event) {
// タイトルが同じものか確認
if($event->summary == $calendar_title){
$st = strtotime($event->start->dateTime);
$et = strtotime($event->end->dateTime);
$res_s_day = date("Y/m/d", $st);
$del_flg = true;
// POSTされたデータにカレンダーの該当日のスケジュールがある場合に内容確認
if (isset($day[$res_s_day])) {
$ts = strtotime($res_s_day." ".$day[$res_s_day]['f']);
$te = strtotime($res_s_day." ".$day[$res_s_day]['t']);
$s = date("Hi", $ts);
$e = date("Hi", $te);
// 内容が同じ場合は、googleカレンダーのスケジュールを削除しない
if ($st == $ts && $et == $te &&
$day[$res_s_day]['remark'] == $event->description) {
// 同じなので、削除しないし、更新もしない
$del_flg = false;
unset($day[$res_s_day]);
}
}
// 削除対象のものをgoogleカレンダーから削除
if($del_flg===true){
// 削除実行
$del_res = $service->events->delete($calendar_id, $event->id);
}
}
}
// 登録・更新する日のスケジュールがない場合終了
if(!isset($day) || !count($day)){
exit;
}
// googleカレンダーにスケジュールを登録します
$optParams = array();
$summary = $calendar_title;
// 対象の日分回します。
foreach ($day as $ymd=>$arr) {
$description = $arr['remark'];
$start = date("c", strtotime($ymd." ".$arr['f']));
$end = date("c", strtotime($ymd." ".$arr['t']));
// 登録するスケジュールデータを作成
$postBody = new Google_Service_Calendar_Event(array(
"summary" => $summary,
"description" => $description,
"start" => array(
"dateTime" => $start,
"timeZone" => "Asia/Tokyo",
),
"end" => array(
"dateTime" => $end,
"timeZone" => "Asia/Tokyo",
),
));
// 登録実行
$results = $service->events->insert($calendar_id, $postBody);
}
//終了
以上です。
これで動かすとエクセルが固まりますので、受け取ったら、非同期処理でgoogleのAPIを実行させる方がいいです。