Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. public function google_api(){
  2. error_reporting(E_ALL);
  3. require_once('google-api/vendor/autoload.php');
  4.  
  5.  
  6. define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
  7. define('CREDENTIALS_PATH', '~/credential/credential.json');
  8. define('CLIENT_SECRET_PATH', __DIR__ . 'google-apiclient_secret.json');
  9.  
  10. // If modifying these scopes, delete your previously saved credentials
  11. // at ~/.credentials/calendar-php-quickstart.json
  12. define('SCOPES', implode(' ', array(
  13. Google_Service_Calendar::CALENDAR_READONLY)
  14. ));
  15.  
  16. // Get the API client and construct the service object.
  17. $client = $this->getClient();
  18. $service = new Google_Service_Calendar($client);
  19.  
  20. // Print the next 10 events on the user's calendar.
  21. $calendarId = 'primary';
  22. $optParams = array(
  23. 'maxResults' => 10,
  24. 'orderBy' => 'startTime',
  25. 'singleEvents' => TRUE,
  26. 'timeMin' => date('c'),
  27. );
  28. $results = $service->events->listEvents($calendarId, $optParams);
  29.  
  30. if (count($results->getItems()) == 0) {
  31. print "No upcoming events found.n";
  32. } else {
  33. print "Upcoming events:n";
  34. foreach ($results->getItems() as $event) {
  35. $start = $event->start->dateTime;
  36. if (empty($start)) {
  37. $start = $event->start->date;
  38. }
  39. printf("%s (%s)n", $event->getSummary(), $start);
  40. }
  41. }
  42. if (php_sapi_name() != 'cli') {
  43. throw new Exception('This application must be run on the command line.');
  44. }
  45.  
  46. }
  47.  
  48. // Code Start
  49. /**
  50. * Returns an authorized API client.
  51. * @return Google_Client the authorized client object
  52. */
  53. function getClient() {
  54. $client = new Google_Client();
  55. $client->setApplicationName(APPLICATION_NAME);
  56. $client->setScopes(SCOPES);
  57. $client->setAuthConfig(CLIENT_SECRET_PATH);
  58.  
  59. $client->setAccessType('offline');
  60.  
  61. // Load previously authorized credentials from a file.
  62. $credentialsPath = $this->expandHomeDirectory(CREDENTIALS_PATH);
  63. if (file_exists($credentialsPath)) {
  64. $accessToken = json_decode(file_get_contents($credentialsPath), true);
  65. } else {
  66. // Request authorization from the user.
  67. $authUrl = $client->createAuthUrl();
  68. printf("Open the following link in your browser:n%sn", $authUrl);
  69. print 'Enter verification code: ';
  70. $authCode = trim(fgets(STDIN));
  71.  
  72. // Exchange authorization code for an access token.
  73. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
  74.  
  75. // Store the credentials to disk.
  76. if(!file_exists(dirname($credentialsPath))) {
  77. mkdir(dirname($credentialsPath), 0700, true);
  78. }
  79. file_put_contents($credentialsPath, json_encode($accessToken));
  80. printf("Credentials saved to %sn", $credentialsPath);
  81. }
  82. $client->setAccessToken($accessToken);
  83.  
  84. // Refresh the token if it's expired.
  85. if ($client->isAccessTokenExpired()) {
  86. $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
  87. file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  88. }
  89. return $client;
  90. }
  91.  
  92. /**
  93. * Expands the home directory alias '~' to the full path.
  94. * @param string $path the path to expand.
  95. * @return string the expanded path.
  96. */
  97. function expandHomeDirectory($path) {
  98. $homeDirectory = getenv('HOME');
  99. if (empty($homeDirectory)) {
  100. $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  101. }
  102. return str_replace('~', realpath($homeDirectory), $path);
  103. }
  104. // Code End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement