Avatar of APD Toronto
APD Toronto
Flag for Canada

asked on 

PHP Fatal Error & GitHub Incomplete Download

Hi Experts,

I had this question after viewing How Do I "Install the Google Client Library".

I guess I closed the above too early, but after downloading from https://github.com/google/google-api-php-client by clicking Clone or Download, then Extracting the .zip, I noticed that all files and folders don't get downloaded (I tried twice),  such as examples/, styles/, tests/, and most files under the root directory when compared to the following screenshot of what is actually downloaded.

GitHub Download
Why aren't all files and folders getting downloaded?

Secondly, and most importantly, after extracting the download into my project director, and running the following code,

<!DOCTYPE html>
<?php

require_once 'google-api/src/Google/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);
  }
}


?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>GMail Calendar Testing</title>
    </head>
    <body>
        
        <h1>GMail Calendar Testing</h1>
        
    </body>
</html>

Open in new window


...I get

PHP Fatal error:  Uncaught Exception: This library must be installed via composer or by downloading the full package. See the instructions at https://github.com/google/google-api-php-client#installation. in C:\inetpub\wwwroot\test\gmail_calendar\google-api\src\Google\autoload.php:14
Stack trace:
#0 C:\inetpub\wwwroot\test\gmail_calendar\index.php(4): require_once()
#1 {main}
  thrown in C:\inetpub\wwwroot\test\gmail_calendar\google-api\src\Google\autoload.php on line 14

Open in new window


If you refer to my original post, you will see  that I'm on a Windows environment and never needed to use GitHub, but I didn't have a chance to test the above until now.

Thank you.
GooglePHPWindows OSMicrosoft IIS Web Server

Avatar of undefined
Last Comment
APD Toronto

8/22/2022 - Mon