3

I have been trying to import/include file in my php script file and somehow it is not working. I know this require_once question has been asked so many times [ 1. require_once with subfolders , 2. require_once() cant find the include path, 3. Using require_once for up directory not working] but none of them seemed to be working for me.

Blow picture will explain what I am trying to do clearly: enter image description here

What I tried and errors:

Attempt 1

require_once(__DIR__.'GoogleClientApi/src/Google/Service/Analytics.php');
echo 'Hey1';  //Does not echo this, means something went wrong above.
require_once(__DIR__.'GoogleClientApi/src/Google/Client.php');
echo 'Hey2';

enter image description here

Error I get:

enter image description here

Attempt 2 (Fixed issue of attempt 1 but raised another issue.)

So I decided to use absolute path :

include '/Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php';
echo 'Hey1';
include '/Applications/MAMP/htdocs/GoogleClientApi/src/Google/Service/Analytics.php';
echo 'Hey2';

Gives me error in Client.php file:

`Warning: require_once(/Google/Auth/AssertionCredentials.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php on line 18`

`Fatal error: require_once(): Failed opening required '/Google/Auth/AssertionCredentials.php' (include_path='.:/Applications/MAMP/bin/php/php5.5.10/lib/php') in /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php on line 18`

Investigating upon I found Client.php (of Google APIs Client Library for PHP) has these lines which are actually present:

require_once 'Google/Auth/AssertionCredentials.php';  // <-- Can't find
require_once 'Google/Cache/File.php';   // <-- Can't find
require_once 'Google/Cache/Memcache.php';  // <-- Can't find

enter image description here

I do not what is going wrong here. Fixed attempt 1's issue but now errors are in google's own library which I do not know how to overcome. I assume I am doing something silly which I can not figure out. Any help or suggestion will be appreciated.

Community
  • 1
  • 1
rohan-patel
  • 5,772
  • 5
  • 45
  • 68
  • Copy the Google Directory and all subdirectories put it in the Same directory as your PHP file you are trying to run. This might help http://www.daimto.com/google-oauth2-php/ – Linda Lawton - DaImTo Jun 19 '14 at 06:44
  • I will check it out and comment. I appreciate your suggestion. – rohan-patel Jun 19 '14 at 14:37
  • @DaImTo : It seems there is an issue with Library files. `Warning: require_once(Google/Auth/OAuth2.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Auth/AssertionCredentials.php on line 18`. Well OAuth2.php and AssertionCredentials.php are in the same directory under **Auth** and it tries to look in to wrong directory using `require_once "Google/Auth/OAuth2.php";` in **AssertionCredentials.php** – rohan-patel Jun 19 '14 at 19:42

3 Answers3

5

Did you ever dumped __DIR__? Usually there's no / at the end. So you would have to change your code to this:

require_once(__DIR__.'/GoogleClientApi/src/Google/Service/Analytics.php');
                      ^

I just checked in your screen shot with the error message, there's no / at the end.

/Applications/MAMP/htdocsGoogleClientApi/src/Google/Client.php instead of /Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php

idmean
  • 14,540
  • 9
  • 54
  • 83
0

It turns out I had to set the include path to include the root directory (src folder) using this line :

set_include_path("/Applications/MAMP/htdocs/GoogleClientApi/src/"); 
rohan-patel
  • 5,772
  • 5
  • 45
  • 68
0

This worked for me in Client.php

set_include_path(__DIR__.'/../' . PATH_SEPARATOR . get_include_path());
mjs
  • 657
  • 7
  • 14