2016-12-29 130 views
2

我想使用日曆ID在我的日曆中插入事件。在谷歌日曆中插入事件

我剛剛從here

複製的代碼片段,並從here

這裏下載SDK是我的代碼

<?php 
// Refer to the PHP quickstart on how to setup the environment: 
// https://developers.google.com/google-apps/calendar/quickstart/php 
// Change the scope to Google_Service_Calendar::CALENDAR and delete any stored 
// credentials. 

include('google-api-php-client-2.0.3/vendor/autoload.php'); 
include('google-api-php-client-2.0.3/src/Google/Client.php'); 
include('google-api-php-client-2.0.3/src/Google/Service/Resource.php'); 


include('google-api-php-client-2.0.3/google-api-php-client-2.0.3/vendor/google/apiclient-services/src/Google/Service/Calendar.php'); 
include('google-api-php-client-2.0.3/google-api-php-client-2.0.3/src/Google/Service.php'); 

$event = new Google_Service_Calendar_Event(array(
    'summary' => 'Google I/O 2015', 
    'location' => '800 Howard St., San Francisco, CA 94103', 
    'description' => 'A chance to hear more about Google\'s developer products.', 
    'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00', 
    'timeZone' => 'America/Los_Angeles', 
), 
    'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00', 
    'timeZone' => 'America/Los_Angeles', 
), 
    'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2' 
), 
    'attendees' => array(
    array('email' => '[email protected]'), 
    array('email' => '[email protected]'), 
), 
    'reminders' => array(
    'useDefault' => FALSE, 
    'overrides' => array(
     array('method' => 'email', 'minutes' => 24 * 60), 
     array('method' => 'popup', 'minutes' => 10), 
    ), 
), 
)); 

$calendarId = '[email protected]'; 

$client = new Google_Client(); 

    $client->setApplicationName("schedular app"); 
    $client->setClientId('xxxxx.apps.googleusercontent.com'); 
    $client->setClientSecret('xxxxxxxxxxxxx'); 


$service = new Google_Service_Calendar_Events_Resource($client,$event,'',''); 
$events = $service->insert($calendarId, $event); 
printf('Event created: %s\n', $event->htmlLink); 



?> 

,但我得到錯誤,如

Fatal error: Cannot declare class Google_Service, because the name is already in use in D:\xampp\htdocs\gmt\google-api-php-client-2.0.3\src\Google\Service.php on line 18 

如何解決這個問題。或者我做錯了什麼。請幫忙。

回答

2

我猜你的問題的根源是,你使用太多包括。只使用include('google-api-php-client-2.0.3/vendor/autoload.php');並刪除其餘的。我覺得你的代碼的結構方式,應該足以創建事件,但我喜歡做的方式是這樣的:

<?php 

session_start(); 

//INCLUDE PHP CLIENT LIBRARY 
require_once "google-api-php-client-2.0.3/vendor/autoload.php"; 

$client = new Google_Client(); 
$client->setAuthConfig("client_creds.json"); 
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php'); 
$client->addScope("https://www.googleapis.com/auth/calendar"); 

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 

    $client->setAccessToken($_SESSION['access_token']); 

    $cal = new Google_Service_Calendar($client); 

    $event = new Google_Service_Calendar_Event(array(
     'summary' => 'Event One', 
     'location' => 'Some Location', 
     'description' => 'Google API Test Event', 
     'start' => array(
     'dateTime' => '2016-11-14T10:00:00-07:00' 
    ), 
     'end' => array(
     'dateTime' => '2016-11-14T10:25:00-07:00' 
    ), 
     'reminders' => array(
     'useDefault' => FALSE, 
     'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60), 
      array('method' => 'popup', 'minutes' => 10), 
     ), 
    ), 
    )); 

    $calendarId = 'primary'; 
    $event = $cal->events->insert($calendarId, $event); 
    printf('Event created: %s\n', $event->htmlLink); 

} else { 

    if (!isset($_GET['code'])) {  

      $auth_url = $client->createAuthUrl(); 
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 

    } else { 

     $client->authenticate($_GET['code']); 
     $_SESSION['access_token'] = $client->getAccessToken(); 

     $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php'; 
     header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 

    } 

} 

?> 

請注意,我用的「的日曆ID值主」,因爲這將是驗證用戶的主日曆。有關更多詳細信息,請參閱文檔here。我希望這有幫助!

0

檢查你的所有,包括文件,這可能會導致此錯誤。您也可以從PHP Quickstart開始:

完成描述了一個簡單的PHP命令行應用程序,它向Google Calendar API發出請求。

這裏是他們的示例代碼:

<?php 
require_once __DIR__ . '/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_READONLY) 
)); 

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); 
    } 
} 

這說明什麼要包含所需的文件。希望這可以幫助。