我正在尝试将Google API PHP客户端库用于Google Analytic v3.
我可以运行我在家里写的简单应用程序,但是当我在办公室试用它时它不起作用.当我运行程序时,我被要求将php应用程序授权给我的谷歌帐户.允许访问后我得到了
Google_IOException:HTTP错误:(0)无法连接到第128行的C:\ wamp\www\google\GoogleClientApi\io\Google_CurlIO.php中的主机
必须连接到我的组织的代理服务器.有谁知道如何使用oauth 2和php客户端库连接到代理服务器.
谢谢
下面是我的php客户端的代码.
session_start();
require_once dirname(__FILE__).'/GoogleClientApi/Google_Client.php';
require_once dirname(__FILE__).'/GoogleClientApi/contrib/Google_AnalyticsService.php';
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setAccessType('online'); // default: offline
$client->setApplicationName('My Application name');
//$client->setClientId(''); omitted for privacy
//$client->setClientSecret(''); omitted for privacy
$client->setRedirectUri($scriptUri);
//$client->setDeveloperKey(''); // API key omitted for privacy
// $service implements the client interface, has to be set before auth call
$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['token']);
die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
die;
}
echo 'Hello, world.';
Run Code Online (Sandbox Code Playgroud)
只是添加(因为我无法在谷歌中找到任何结果)如果你想避免编辑库本身你可以通过$ client对象指定额外的curl参数.这样做的代码看起来大致如此.
$client = new Google_Client();
$client->getIo()->setOptions(array(
CURLOPT_PROXY => 'myproxy.mywebsite.com',
CURLOPT_PROXYPORT => 8909
));
Run Code Online (Sandbox Code Playgroud)