此軟件包使與Google日曆一起工作變得輕而易舉。設置後,您可以做這些事情:
use Spatie GoogleCalendar Event ;
// create a new event
$ event = new Event ;
$ event -> name = ' A new event ' ;
$ event -> description = ' Event description ' ;
$ event -> startDateTime = Carbon Carbon:: now ();
$ event -> endDateTime = Carbon Carbon:: now ()-> addHour ();
$ event -> addAttendee ([
' email ' => ' [email protected] ' ,
' name ' => ' John Doe ' ,
' comment ' => ' Lorum ipsum ' ,
' responseStatus ' => ' needsAction ' ,
]);
$ event -> addAttendee ([ ' email ' => ' [email protected] ' ]);
$ event -> addMeetLink (); // optionally add a google meet link to the event
$ event -> save ();
// get all future events on a calendar
$ events = Event:: get ();
// update existing event
$ firstEvent = $ events -> first ();
$ firstEvent -> name = ' updated name ' ;
$ firstEvent -> save ();
$ firstEvent -> update ([ ' name ' => ' updated again ' ]);
// create a new event
Event:: create ([
' name ' => ' A new event ' ,
' startDateTime ' => Carbon Carbon:: now (),
' endDateTime ' => Carbon Carbon:: now ()-> addHour (),
]);
// delete an event
$ event -> delete ();Spatie是位於比利時安特衛普的網絡設計代理商。您會在網站上找到我們所有開源項目的概述。
我們投入大量資源來創建課堂開源軟件包。您可以通過購買我們的付費產品之一來支持我們。
非常感謝您向我們發送您的家鄉明信片,並提到您正在使用的包裹。您將在我們的聯繫頁面上找到我們的地址。我們在虛擬明信片牆上發布了所有收到的明信片。
您可以通過作曲家安裝軟件包:
composer require spatie/laravel-google-calendar您必須使用此命令發布配置:
php artisan vendor:publish --provider= " SpatieGoogleCalendarGoogleCalendarServiceProvider "這將在您的config-directory中發布一個名為google-calendar.php的文件,其中包含以下內容:
return [
' default_auth_profile ' => env ( ' GOOGLE_CALENDAR_AUTH_PROFILE ' , ' service_account ' ),
' auth_profiles ' => [
/*
* Authenticate using a service account.
*/
' service_account ' => [
/*
* Path to the json file containing the credentials.
*/
' credentials_json ' => storage_path ( ' app/google-calendar/service-account-credentials.json ' ),
],
/*
* Authenticate with actual google user account.
*/
' oauth ' => [
/*
* Path to the json file containing the oauth2 credentials.
*/
' credentials_json ' => storage_path ( ' app/google-calendar/oauth-credentials.json ' ),
/*
* Path to the json file containing the oauth2 token.
*/
' token_json ' => storage_path ( ' app/google-calendar/oauth-token.json ' ),
],
],
/*
* The id of the Google Calendar that will be used by default.
*/
' calendar_id ' => env ( ' GOOGLE_CALENDAR_ID ' ),
];您需要做的第一件事是獲得使用Google API的憑據。我假設您已經創建了一個Google帳戶並已登錄。前往Google API控制台,然後在標題中單擊“選擇項目”。

接下來,我們必須指定該項目可能會消費的API。從標題中選擇“啟用API和服務”。

在下一頁上,搜索“日曆”,然後從列表中選擇“ Google Calendar API”。

從這裡開始,按“啟用”以啟用該項目的Google日曆API。

現在,您已經創建了一個可以訪問日曆API的項目了,該下載具有這些憑據的文件了。單擊側邊欄中的“憑據”,然後按“ API&Services中的憑據”鏈接。

在此頁面上,打開“創建憑據”下拉曲,然後選擇“服務帳戶密鑰”。

在下一個屏幕上,您可以給服務帳戶一個名稱。您可以命名任何想要的東西。在服務帳戶ID中,您會看到一個電子郵件地址。我們將在本指南稍後使用此電子郵件地址。選擇“ JSON”作為密鑰類型,然後單擊“創建”以下載JSON文件。您將警告說,服務帳戶沒有角色,您可以安全地忽略它並創建服務帳戶而無需分配角色。
如果您已委派了範圍內對服務帳戶的訪問權,並且想模仿用戶帳戶,請在配置文件中指定用戶帳戶的電子郵件地址。

將JSON在您的Laravel項目中保存在此軟件包的配置文件的service_account_credentials_json鍵中指定的位置。由於JSON文件包含潛在的敏感信息,因此我不建議將其提交給您的GIT存儲庫。
現在,一切都在API網站上設置了,我們需要在Google日曆網站上配置某些內容。前往Google日曆,並查看要通過PHP使用的日曆的設置。在“與特定人員共享”選項卡上,按“添加人員”按鈕,並添加在API站點上創建憑據時顯示的服務帳戶ID。


向下滾動到“集成日曆”部分以查看日曆的ID。您需要在配置文件中指定該ID。

該軟件包支持OAuth2身份驗證。這使您可以通過實際的Google帳戶進行身份驗證,並使用自己的Google帳戶創建和管理事件。
OAuth2身份驗證還需要一個令牌文件,除了憑據文件。生成這兩個文件的最簡單方法是使用PHP QuickStart工具。 token.json本指南將生成兩個credentials.json :它們必須分別以oauth-credentials.json和oauth-token.json的形式保存到您的項目中。在此軟件包中檢查配置文件,以獲取有關在哪裡保存這些文件的確切詳細信息。
要使用OAuth2,您還必須在.env文件中設置一個新的環境變量:
GOOGLE_CALENDAR_AUTH_PROFILE =oauth如果您正在從此軟件包的較舊版本中升級,則需要強製配置發布:
php artisan vendor:publish --provider= " SpatieGoogleCalendarGoogleCalendarServiceProvider " --force最後,要在您的應用程序中獲得更無縫的體驗,而不是使用Quickstart工具,您可以在Google API控制台中設置同意屏幕。這將允許您應用程序的非技術用戶輕鬆生成自己的令牌。這是完全可選的。
您可以通過簡單地調用Event::get();這將返回來年的所有活動。事件以SpatieGoogleCalendarEvent對象的形式出現。
該功能的完整簽名是:
public static function get( Carbon $ startDateTime = null , Carbon $ endDateTime = null , array $ queryParameters = [], string $ calendarId = null ): Collection您可以在$queryParameters中傳遞的參數在Google日曆API文檔list上的文檔中列出。
您可以使用這些Getters作為碳實例檢索開始日期和結束日期:
$ events = Event:: get ();
$ events [ 0 ]-> startDate ;
$ events [ 0 ]-> startDateTime ;
$ events [ 0 ]-> endDate ;
$ events [ 0 ]-> endDateTime ;您只需介紹一個SpatieGoogleCalendarEvent -object
$ event = new Event ;
$ event -> name = ' A new event ' ;
$ event -> startDateTime = Carbon Carbon:: now ();
$ event -> endDateTime = Carbon Carbon:: now ()-> addHour ();
$ event -> save ();您也可以靜態地調用create :
Event:: create ([
' name ' => ' A new event ' ,
' startDateTime ' => Carbon Carbon:: now (),
' endDateTime ' => Carbon Carbon:: now ()-> addHour (),
]);這將創建一個具有特定開始和結束時間的事件。如果要創建一個全日事件,則必須使用startDate和endDate而不是startDateTime和endDateTime 。
$ event = new Event ;
$ event -> name = ' A new full day event ' ;
$ event -> startDate = Carbon Carbon:: now ();
$ event -> endDate = Carbon Carbon:: now ()-> addDay ();
$ event -> save ();您可以基於這樣的簡單文本字符串創建事件:
$ event = new Event ();
$ event -> quickSave ( ' Appointment at Somewhere on April 25 10am-10:25am ' );
// statically
Event:: quickCreate ( ' Appointment at Somewhere on April 25 10am-10:25am ' );Google為每個事件分配一個唯一的ID。您可以通過使用get方法獲取事件並在SpatieGoogleCalendarEvent -Object上獲得id屬性來獲得此ID:
// get the id of the first upcoming event in the calendar .
$ eventId = Event:: get ()-> first ()-> id ;
// you can also get the id after creating the event , then you can save it to database .
$ event = new Event ;
$ newEvent = $ event -> save ();
echo $ newEvent -> id ; // display the event id您可以使用此ID從Google獲取單個事件:
Event:: find ( $ eventId );簡單,只需更改一些屬性,然後調用save() :
$ event = Event:: find ( $ eventId );
$ event -> name = ' My updated title ' ;
$ event -> save ();另外,您可以使用更新方法:
$ event = Event:: find ( $ eventId );
$ event -> update ([ ' name ' => ' My updated title ' ]);沒什麼!
$ event = Event:: find ( $ eventId );
$ event -> delete ();您可以在事件中設置源URL,僅對事件的創建者可見(有關源屬性的更多信息,請參見文檔)。此功能僅在通過OAuth進行身份驗證時起作用。
$ yourEvent -> source = [
' title ' => ' Test Source Title ' ,
' url ' => ' http://testsource.url ' ,
];您可以為活動設置某些顏色(Colorid 1至11)。可能性僅限於Google日曆API的顏色定義。您可以在這裡找到它們。
$ yourevent -> setColorId ( 11 );Google日曆API提供了許多選項。此軟件包並不支持所有包裝。例如,本軟件包無法正確管理重複事件。如果您堅持使用名稱和日期創建事件,那麼您應該可以。
v1和v2之間的唯一主要區別是,在引擎蓋下使用Google API V2而不是V1。以下是升級所需的步驟:
laravel-google-calendar重命名config File到google-calendarclient_secret_json鍵to service_account_credentials_json 有關最近發生了變化的更多信息,請參見ChangElog。
composer test 請有關詳細信息,請參閱貢獻。
如果發現任何與安全有關的問題,請發送電子郵件至[email protected],而不是使用問題跟踪器。
非常感謝Sebastiaan Luca為創建此包裝的V2的巨大幫助。
麻省理工學院許可證(麻省理工學院)。請參閱許可證文件以獲取更多信息。