├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── BaseClass.php ├── Calendar.php └── Events.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | node_modules/ 3 | 4 | # Laravel 4 specific 5 | bootstrap/compiled.php 6 | app/storage/ 7 | 8 | # Laravel 5 & Lumen specific 9 | bootstrap/cache/ 10 | storage/ 11 | .env.*.php 12 | .env.php 13 | .env 14 | .env.example -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Askedio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel5-Google-Calendar 2 | A simple integration of Laravel 5 (or any PHP) and Google Calendar REST API 3 | 4 | Used in the Laravel Bill Reminder app I created: 5 | https://github.com/Askedio/BillReminder 6 | 7 | # Overview 8 | I needed access to Google Calendar and their PHP SDK failed for me on to many levels. I am perfectly happy using REST APIS and decided to go that route. 9 | 10 | 11 | ## This package needs better authentication checks, error checks and unit testing. 12 | 13 | # Usage 14 | 15 | Declare use statements to access Calendar and CalendarEvent. 16 | ~~~ 17 | use Askedio\Laravel5GoogleCalendar\Calendar; 18 | use Askedio\Laravel5GoogleCalendar\Events as CalendarEvent; 19 | ~~~ 20 | 21 | 22 | # Event Options 23 | 24 | ~~~ 25 | CalendarEvent::createEvents([]); 26 | CalendarEvent::readEvents($eventId=false); # false to read all events in time frame 27 | CalendarEvent::updateEvents($eventId, []); 28 | CalendarEvent::deleteEvents($eventId); 29 | ~~~ 30 | 31 | # Calendar Options 32 | 33 | ~~~ 34 | Calendar::createCalendar([]); 35 | Calendar::readCalendar(); 36 | Calendar::updateCalendar([]); 37 | Calendar::deleteCalendar(); 38 | ~~~ 39 | 40 | # Class Options 41 | Set the calendar to an empty value when using readCalendar to get a list of calendars. 42 | 43 | ~~~ 44 | # Change calendar 45 | Calendar::setVar('calendar', 'primary'); 46 | 47 | # Change start/end time 48 | Calendar::setVar('start', 'yesterday'); 49 | Calendar::setVar('end', 'today'); 50 | ~~~ 51 | 52 | 53 | # Creating & Updating 54 | Creating sends a POST request with the array provided. Updating sents a PATCH request with the array provided. 55 | 56 | Check the Google Calendar API documentation for the values to be provided. You can also view mine here: 57 | https://github.com/Askedio/BillReminder/blob/master/src/app/Helpers/BillReminder.php 58 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "askedio/laravel5-google-calendar", 3 | "description": "Google Calendar REST API for Laravel 5", 4 | "keywords": ["laravel", "google calendar"], 5 | "license": "MIT", 6 | "type": "library", 7 | "require": { 8 | "php": ">=5.5.9", 9 | "laravel/framework": "5.2.*" 10 | }, 11 | "autoload": { 12 | "psr-4": { 13 | "Askedio\\Laravel5GoogleCalendar\\": "src/" 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/BaseClass.php: -------------------------------------------------------------------------------- 1 | token, 108 | ]; 109 | } 110 | 111 | /** 112 | * Return url for get/post call, has request vars + api key. 113 | * 114 | * @return string 115 | */ 116 | private static function get_url() 117 | { 118 | return self::$google_api.self::$url.self::build_request(); 119 | } 120 | 121 | /** 122 | * Return result of curl get/post. 123 | * 124 | * @return object/false 125 | */ 126 | private static function curl() 127 | { 128 | $ch = self::curl_open(); 129 | $results = json_decode(curl_exec($ch)); 130 | curl_close($ch); 131 | 132 | return is_object($results) ? self::check_errors($results) : false; 133 | } 134 | 135 | /** 136 | * Return curl. 137 | * 138 | * @return curl_init(); 139 | */ 140 | private static function curl_open() 141 | { 142 | $ch = curl_init(); 143 | curl_setopt($ch, CURLOPT_URL, self::get_url()); 144 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 145 | curl_setopt($ch, CURLOPT_HTTPHEADER, self::curl_headers()); 146 | if (self::$curl_method == 'POST') { 147 | curl_setopt($ch, CURLOPT_POST, true); 148 | } 149 | if (in_array(self::$curl_method, ['DELETE', 'PUT', 'PATCH'])) { 150 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, self::$curl_method); 151 | } 152 | if (in_array(self::$curl_method, ['POST', 'PUT', 'PATCH'])) { 153 | curl_setopt($ch, CURLOPT_POSTFIELDS, self::$curl_data); 154 | } 155 | 156 | return $ch; 157 | } 158 | 159 | /** 160 | * Return boolean if error exists on curl result. 161 | * 162 | * @return bool 163 | */ 164 | private static function check_errors($results) 165 | { 166 | return isset($results->error->errors) 167 | ? self::setVar('errors', $results->error->errors) 168 | : $results; 169 | } 170 | 171 | /** 172 | * Return request query. 173 | * 174 | * @return http_build_query() 175 | */ 176 | private static function build_request() 177 | { 178 | $_key = env('GOOGLE_API_KEY') ? ['key' => env('GOOGLE_API_KEY')] : []; 179 | 180 | return '?'.http_build_query(array_merge(self::$request, $_key)); 181 | } 182 | 183 | /** 184 | * Return date RFC3339. 185 | * 186 | * @return false 187 | */ 188 | public static function get_time($time = 'today') 189 | { 190 | $dt = new \DateTime($time, new \DateTimeZone(config('timezone', 'America/Los_Angeles'))); 191 | 192 | return $dt->format('Y-m-d\TH:i:sP'); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/Calendar.php: -------------------------------------------------------------------------------- 1 | self::get_time(self::$start), 26 | 'timeMax' => self::get_time(self::$end), 27 | 'singleEvents' => 'true', 28 | ]); 29 | 30 | return self::events('get', false, $event); 31 | } 32 | 33 | /** 34 | * Return json results when updating a calendar. 35 | * 36 | * @return object() 37 | */ 38 | public static function updateEvents($event, $data = []) 39 | { 40 | return self::events('patch', $data, $event); 41 | } 42 | 43 | /** 44 | * Return json results when removing a calendar. 45 | * 46 | * @return object() 47 | */ 48 | public static function deleteEvents($event) 49 | { 50 | return self::events('remove', false, $event); 51 | } 52 | 53 | /** 54 | * Return calendar function based on method. 55 | * 56 | * @return object() 57 | */ 58 | private static function events($method, $data, $event = false) 59 | { 60 | self::setVar('url', '/calendars/'.self::$calendar.'/events/'.$event); 61 | 62 | return self::$method($data); 63 | } 64 | } 65 | --------------------------------------------------------------------------------