PHP API客户端中的required_once问题

时间:2014-06-18 19:20:21

标签: php google-analytics include-path google-api-php-client require-once

我一直在尝试在我的php脚本文件中导入/包含文件,不知何故它无法正常工作。我知道{require_once1. require_once with subfolders 2. require_once() cant find the include path已经多次询问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'; 个问题了,但似乎没有一个问题适合我。

Blow图片将解释我想要做的事情: enter image description here

我尝试了什么和错误:

尝试1

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

enter image description here

我得到错误:

enter image description here

尝试2(修复了尝试1的问题但提出了另一个问题。)

所以我决定使用绝对路径:

`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

在Client.php文件中给我错误:

{{1}}

enter image description here

我不知道这里出了什么问题。修复了尝试1的问题,但现在错误发生在google自己的库中,我不知道如何克服。我想我正在做一些我无法弄清楚的傻​​事。任何帮助或建议将不胜感激。

3 个答案:

答案 0 :(得分:5)

你有没有抛弃__DIR__?通常最后没有/。因此,您必须将代码更改为:

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

我刚刚检查了您的screen shot with the error message,其中没有/最后。

/Applications/MAMP/htdocsGoogleClientApi/src/Google/Client.php 而不是/Applications/MAMP/htdocs/GoogleClientApi/src/Google/Client.php

答案 1 :(得分:0)

事实证明我必须使用以下行将include路径设置为包含根目录(src文件夹)

set_include_path("/Applications/MAMP/htdocs/GoogleClientApi/src/"); 

答案 2 :(得分:0)

这在Client.php

中对我有用
set_include_path(__DIR__.'/../' . PATH_SEPARATOR . get_include_path());