--
PHP 版本程式範例
注意事項
- 一定要 PHP 5.4 以上版本 (稍微看一下在 5.3 上運作的錯誤碼,應該沒辦法用 5.3 改寫)
- 不需要 Composer ,只要到 Github 下載壓縮檔解壓縮就可以了 google-api-php-client-2.2.2_PHP54.zip
- 到 https://developers.google.com/calendar/quickstart/php 點選 ENABLE THE GOOGLE CALENDAR API 按鈕下載 credentials.json 檔案
- 將 credentials.json 檔案放置到 php 程式內 CLIENT_SECRET_PATH 定義路徑以及檔名,例如以下程式就必須將檔名改成 client_secret.json
- 到 CLI 執行下列 php 程式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
<?php date_default_timezone_set('Asia/Taipei'); require_once __DIR__ . '/../Component_Back/google-api-php-client-2.2.2_PHP54/vendor/autoload.php'; define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart'); define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json'); define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/calendar-php-quickstart.json define('SCOPES', implode(' ', array( Google_Service_Calendar::CALENDAR) )); if (php_sapi_name() != 'cli') { throw new Exception('This application must be run on the command line.'); } /** * Returns an authorized API client. * @return Google_Client the authorized client object */ function getClient() { $client = new Google_Client(); $client->setApplicationName(APPLICATION_NAME); $client->setScopes(SCOPES); $client->setAuthConfig(CLIENT_SECRET_PATH); $client->setAccessType('offline'); // Load previously authorized credentials from a file. $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); if (file_exists($credentialsPath)) { $accessToken = json_decode(file_get_contents($credentialsPath), true); } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); // Exchange authorization code for an access token. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); // Store the credentials to disk. if(!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, json_encode($accessToken)); printf("Credentials saved to %s\n", $credentialsPath); } $client->setAccessToken($accessToken); // Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($client->getAccessToken())); } return $client; } /** * Expands the home directory alias '~' to the full path. * @param string $path the path to expand. * @return string the expanded path. */ function expandHomeDirectory($path) { $homeDirectory = getenv('HOME'); if (empty($homeDirectory)) { $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); } return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. $client = getClient(); $service = new Google_Service_Calendar($client); // Print the next 10 events on the user's calendar. $calendarId = 'primary'; $optParams = array( 'maxResults' => 10, 'orderBy' => 'startTime', 'singleEvents' => TRUE, 'timeMin' => date('c'), ); $results = $service->events->listEvents($calendarId, $optParams); if (count($results->getItems()) == 0) { print "No upcoming events found.\n"; } else { print "Upcoming events:\n"; foreach ($results->getItems() as $event) { $start = $event->start->dateTime; if (empty($start)) { $start = $event->start->date; } printf("%s (%s)\n", $event->getSummary(), $start); } } |
環境設置成功回出現下列畫面
1 2 3 4 |
[root@mail:x] # php m.php Open the following link in your browser: https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=862255922531-phf02o6e7e11gsoh7mpdh326jucct27o.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly&approval_prompt=auto Enter verification code: |
將顯示網址貼到瀏覽器授權使用之後即可出現授權碼,複製貼上即可
授權完成,access_token 會儲存在 CREDENTIALS_PATH 定義路徑內
--
發出邀請給同 G Suite 的其他人
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?php date_default_timezone_set('Asia/Taipei'); require_once __DIR__ . '/../Component_Back/google-api-php-client-2.2.2_PHP54/vendor/autoload.php'; define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart'); define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json'); define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); // If modifying these scopes, delete your previously saved credentials // at ~/.credentials/calendar-php-quickstart.json // CALENDAR_READONLY define('SCOPES', implode(' ', array( Google_Service_Calendar::CALENDAR) )); if (php_sapi_name() != 'cli') { throw new Exception('This application must be run on the command line.'); } /** * Returns an authorized API client. * @return Google_Client the authorized client object */ function getClient() { $client = new Google_Client(); $client->setApplicationName(APPLICATION_NAME); $client->setScopes(SCOPES); $client->setAuthConfig(CLIENT_SECRET_PATH); $client->setAccessType('offline'); // Load previously authorized credentials from a file. $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); if (file_exists($credentialsPath)) { $accessToken = json_decode(file_get_contents($credentialsPath), true); } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); // Exchange authorization code for an access token. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); // Store the credentials to disk. if(!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, json_encode($accessToken)); printf("Credentials saved to %s\n", $credentialsPath); } $client->setAccessToken($accessToken); // Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($client->getAccessToken())); } return $client; } /** * Expands the home directory alias '~' to the full path. * @param string $path the path to expand. * @return string the expanded path. */ function expandHomeDirectory($path) { $homeDirectory = getenv('HOME'); if (empty($homeDirectory)) { $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); } return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. $client = getClient(); $service = new Google_Service_Calendar($client); $event = new Google_Service_Calendar_Event(array( 'summary' => 'OiKID 日曆活動測試', 'location' => '800 Howard St., San Francisco, CA 94103', 'description' => 'A chance to hear more about Google\'s developer products.', 'start' => array( 'dateTime' => '2019-02-20T09:00:00-07:00', 'timeZone' => 'America/Los_Angeles', ), 'end' => array( 'dateTime' => '2019-02-20T17:00:00-07:00', 'timeZone' => 'America/Los_Angeles', ), //'recurrence' => array( // 'RRULE:FREQ=DAILY;COUNT=2' //), 'attendees' => array( array('email' => 'fb01@hoyo.idv.tw'), array('email' => 'phenix@sly-ha.com.tw'), ), 'reminders' => array( 'useDefault' => FALSE, 'overrides' => array( array('method' => 'email', 'minutes' => 24 * 60), array('method' => 'popup', 'minutes' => 10), ), ), )); $calendarId = 'primary'; $event = $service->events->insert($calendarId, $event); printf('Event created: %s\n', $event->htmlLink); |
--
踩到的 bug
php.ini
1 |
curl.cainfo = "c:\bin\php56\cacert.pem" |
--
成果
1 2 3 |
C:\bin\php56\html>..\php c.php Upcoming events: aaaa (2018-02-07T11:30:00+08:00) |
--
使用網址加入自己的日曆
1 |
https://www.google.com/calendar/render?action=TEMPLATE&trp=true&sf=true&output=xml&dates=20140711T120000Z/20140711T143000Z&details=就是教不落%0Ahttps://steachs.com&location=101大樓101F&text=加入就是教不落粉絲團 |
連結後確認按下儲存即可加入自己的日曆
另一個範例
1 |
http://www.google.com/calendar/event?action=TEMPLATE&text=2019+4%E6%9C%88%E4%BB%BD+SA%40Taipei+%E5%A4%A7%E6%95%B8%E6%93%9A%E7%9A%84%E5%AF%A6%E8%B8%90%EF%BC%8C%E9%99%A4%E4%BA%86%E6%8A%80%E8%A1%93%EF%BC%8C%E9%82%84%E9%9C%80%E8%A6%81%E4%BB%80%E9%BA%BC%EF%BC%9F&dates=20190420T050000Z/20190420T070000Z&details=https://studyarea.kktix.cc/events/33b9e0e6-copy-1&location=%E5%8F%B0%E5%8C%97%E5%B8%82%E5%85%A7%E6%B9%96%E5%8D%80%E6%B4%B2%E5%AD%90%E8%A1%97196%E8%99%9F&trp=true&sprop=https://studyarea.kktix.cc/events/33b9e0e6-copy-1&sprop=name:KKTIX |
--
4,372 total views, 1 views today