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 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>

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(

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

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

  // Refresh the token if it's expired.
  if ($client->isAccessTokenExpired()) {
    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);

        <meta charset="UTF-8">
        <title>GMail Calendar Testing</title>
        <h1>GMail Calendar Testing</h1>

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 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.
APD TorontoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott FellDeveloper & EE ModeratorCommented:
I posted an update to your other question.    Start over, and install via using composer.

Start at the console if you have not already.  Add your project there, then download the sdk via composer.  The instructions are There are also some code examples.  

On github, there are also instructions there as well for installing via composer. 
composer require google/apiclient:^2.0

Open in new window

When everything gets downloaded using composer, composer will take care of auto loading all the other files required with the autoload.

Once you have all of this installed. Just remember making an api call is a two step process. First you are submitting your own credentials to get a token which gets returned in the first call. Then submit the token with each subsequent api call.
APD TorontoSoftware DeveloperAuthor Commented:
Thanks Scott, I have gone through, which is a good refresher, but is only theory and does not pprovide detailed instructions.

I think you are saying that I need to install the Composer, then install the SDK through the composer.

Based on that, what is the Composer, is that Google, what does it do, and where do I get it?

What is
composer require google/apiclient:^2.0

Open in new window

Remember, I'm not familiar with MAC, Linux and GitHub - do I need to be?
APD TorontoSoftware DeveloperAuthor Commented:
With a little research, I found that Composer is a dependency manager, that takes care of updates. Plus, composer needs to be installed on the server, but this particular application is going on a shared hosting.  What if they don't have Composer?

Can I not use the Google Calendar SDK (or, just API) without using Composer?

I understand that using Composer might be preferred and a personal preference, but can I not just download the files and just use them, similar  to PHPMailer or TCPDF?
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Chris StanyonWebDevCommented:
You're correct in your assumption that Composer is a dependency manager. The download you've grabbed is just the src folder and doesn't include the dependencies which is why they suggest installing it via Composer. That's part of the beauty of Composer - devs don't need to ship the project dependencies - just a composer.json file.

Generally with raw source code like this, the idea is that you clone the repo and then run Composer - you then have the full stack set up and ready to go.

As for running it on a server, you could if you wanted to, just install it locally using Composer - that would pull all the relevant deps into your local environment, and you could then develop and test locally. Once your dev is done, you could upload all relevant files to the Server manually.

Alternatively, there is a Releases page available for the API which includes all the relevant dependencies, so it may be better for you to just download a Release that's already packaged - heres the page you need -> - you'll probably want 2.2.0
APD TorontoSoftware DeveloperAuthor Commented:
I downloaded the packaged release, and received the following error

PHP Fatal error:  Uncaught Exception: This application must be run on the command line. in C:\inetpub\wwwroot\test\gmail_calendar\index.php:17
Stack trace:
#0 {main}
  thrown in C:\inetpub\wwwroot\test\gmail_calendar\index.php on line 17

Open in new window

I, then deleted all API folders from my project, installed Composer, then installed the Google API through the Composer, and I got the same error.
Chris StanyonWebDevCommented:
OK - the error should be fairly self-explanatory. You're trying to call your page in a web browser when you should be calling it from the command line (CLI). If you have PHP setup correctly on your windows machine, then from the CLI you need to type the following:

php index.php
APD TorontoSoftware DeveloperAuthor Commented:
From the command line, when I run it it is asking me for verification code? Where do I get that?

As well, why is it only working on the command line, when it is a web application?
Chris StanyonWebDevCommented:
OK. Reading through your code, it looks like it should ask you to visit a URL ("Open the following link in your browser"). When you open that link, it will ask you to log in to Google and Authorise your application (using oAuth). Once you've done that it will redirect back to whatever URL you setup as the callback when you created the API Credentials in your Google API Console. That URL will include a querystring that contains the verification code (yourCallback.php?code=xxxxxxxxxxx).

The reason it's only working on the command line is because that's what your code tells it to do!!

if (php_sapi_name() != 'cli') {
  throw new Exception('This application must be run on the command line.');

Open in new window

APD TorontoSoftware DeveloperAuthor Commented:
Copying that oAth in my command line worked, but:

1- When I comment out throw new exception...and run it in my browser I get

PHP Notice:  Use of undefined constant STDIN - assumed 'STDIN' in C:\inetpub\wwwroot\test\gmail_calendar\index.php on line 40
PHP Warning:  fgets() expects parameter 1 to be resource, string given in C:\inetpub\wwwroot\test\gmail_calendar\index.php on line 40
PHP Fatal error:  Uncaught InvalidArgumentException: Invalid code in C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\Google\Client.php:176
Stack trace:
#0 C:\inetpub\wwwroot\test\gmail_calendar\index.php(43): Google_Client->fetchAccessTokenWithAuthCode('')
#1 C:\inetpub\wwwroot\test\gmail_calendar\index.php(76): getClient()
#2 {main}
  thrown in C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\Google\Client.php on line 176

Why is this, and why is it a part of the original sample code provided at

I should be intended to be a web application, so why it is looking for the command line by default, should it not be a quick start sample to get you going?
Chris StanyonWebDevCommented:
OK. You can't just comment out the Exception line and expect it to work. The whole code is designed to run in the Command Line and NOT in a web browser. If you look at the QuickStart page you've linked to, the very first paragraph states:

Complete the steps described in the rest of this page, and in about five minutes you'll have a simple PHP command-line application that makes requests to the Google Calendar API.

It clearly says that you will have a simple PHP command-line application!

If you want to develop a web-app then you're going to need to code it very differently. I would start by referring to the API Client docs -

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Thanks Chris, I'll continue with your recommended link.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.