R
R
robert_n2016-02-02 15:09:33
Google
robert_n, 2016-02-02 15:09:33

Google Api Client for PHP and Composer library?

Hello!
There was a problem adding the Google Api Client for PHP library using Composer. I ask for your help!
Before that, I did not encounter Composer, and only today I installed it, following this instruction . Composer starts, tested with the command:
$ composer
Next, I added the Google library:
composer require google/apiclient:^[email protected]
The library was pulled up, here is the content of composer.json

{
    "require": {
        "google/apiclient": "^[email protected]"
    }
}

The folder and file structure is as follows:
─root
  ├─.composer(папка)
  ├─composer.lock
  ├─composer.json
  └─vendor(папка)
    ├─autoload.php
    ├─composer(папка)
    └─google(папка)
      └─...
─var
  ├─www(папка)
    └─html(папка)
      └─index.php

Added the following code to the index.php file:
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("YOUR_APP_KEY");

$service = new Google_Service_Books($client);
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);

foreach ($results as $item) {
  echo $item['volumeInfo']['title'], "<br /> \n";
}

When loading index.php the server throws 'Error 500'. The logs say:
PHP Warning:  require_once(vendor/autoload.php): failed to open stream: No such file or directory in 
/var/www/html/index.php on line 3

PHP Fatal error:  require_once(): Failed opening required 'vendor/autoload.php' 
(include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/index.php on line 3

Please tell me what I did wrong? Maybe Composer should have been installed in the html folder? Or should the autoload.php file be moved? I thought that the setup would take 10-15 minutes, but I've been trying to figure it out for a whole day and I still can't solve this problem ((

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wol_fi, 2016-02-02
@robert_n

require_once 'vendor/autoload.php';
This would work if you had index.php in root.
try

require_once '../../../../root/vendor/autoload.php';

P
Philipp, 2016-02-13
@zoonman

I can recommend you to make the project structure in the style

public/index.php  - основной файл
src/ - здесь весь ваш код
composer.json - композер, при запуске обновления сам создаст папку вендор

You will have to reconfigure the server to serve files from public.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question