Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Created March 21, 2024 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tranchausky/2a54abba11935dcae3b502c1b7d49eab to your computer and use it in GitHub Desktop.
Save tranchausky/2a54abba11935dcae3b502c1b7d49eab to your computer and use it in GitHub Desktop.
get 8 day data from google calader (Use this URL to access this calendar from a web browser.)
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$input = 'https://calendar.google.com/calendar/embed?src=a56ac6da1d0184b389b3f590ba25b686b1bc6580f5666b0c6ae63259b31cbb1b%40group.calendar.google.com&ctz=Asia%2FHo_Chi_Minh';
$resl = builLink($input);
var_dump($resl);
function builLink($calendarId){
$id = getIdFromPublic($calendarId);
$arrDefault = getArrayDefault();
$data = array_merge($arrDefault, []);
$arrCondition = getArrayCondition();
$data = array_merge($data, $arrCondition);
$data['key'] = getkey($calendarId);
$rs = getRequestLink($id, $data);
return $rs;
}
function getRequestLink($id = '', $more = []){
$link = 'https://clients6.google.com/calendar/v3/calendars/'.$id.'@group.calendar.google.com/events?calendarId='.$id.'%40group.calendar.google.com';
//$strmore = implode('&',$more);
$strmore = http_build_query($more);
return $link.'&'.$strmore;
}
function getkey($url){
$html = file_get_contents($url);
// Define the regular expression pattern to match the developerKey
$pattern = '/"developerKey":"(.*?)"/';
// Use preg_match to find the developerKey in the HTML content
if (preg_match($pattern, $html, $matches)) {
$developerKey = $matches[1];
return $developerKey;
} else {
return '';
}
}
//https: //calendar.google.com/calendar/embed?src=a56ac6da1d0184b389b3f590ba25b686b1bc6580f5666b0c6ae63259b31cbb1b%40group.calendar.google.com&ctz=Asia%2FHo_Chi_Minh
function getIdFromPublic($input){
$data = parse_url($input);
parse_str($data['query'], $par);
$id = '';
if(!empty($par['src'])){
$array = explode('@', $par['src']);
if(isset($array[0])){
$id = $array[0];
}
}
return $id;
}
function getArrayLink($link = ''){
$data = parse_url($link);
parse_str($data['query'], $par);
return $par;
}
function getArrayCondition(){
$now = new DateTime();
$lastDay = $now->modify('-1 day')->format('Y-m-d\TH:i:s.u\Z');
$next7Day = $now->modify('+7 day')->format('Y-m-d\TH:i:s.u\Z');
$data = [
'timeMin' => $lastDay,
'timeMax' => $next7Day,
//'timeMin' => '2024-02-25T00:00:00+07:00',
//'timeMax' => '2024-04-07T00:00:00+07:00',
'$unique' => 'gc456',
];
return $data;
}
function getArrayDefault(){
$data = [
'singleEvents' => 'true',
'eventTypes' => 'default',
//'eventTypes' => 'outOfOffice',//focusTime
'timeZone' => 'Asia/Ho_Chi_Minh',
//'maxAttendees' => '1',
'maxResults' => '250',
'sanitizeHtml' => 'true',
//'key' => 'AIzaSyBNlYH01_9Hc5S1J9vuFmu2nUqBZJNAXxs',
];
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment