我正在尝试将 Google API PHP Client 库用于 Google Analytic v3。
我能够运行我在家里编写的简单应用程序,但是当我在办公室尝试时它不起作用。当我运行程序时,我被要求将 php 应用程序授权给我的谷歌帐户。允许访问后,我得到
Google_IOException: HTTP Error: (0) could not connect to host in C:\wamp\www\google\GoogleClientApi\io\Google_CurlIO.php on line 128
有必要连接到我组织的代理服务器。有谁知道如何使用 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.';