问题
I am trying to use the Google API PHP Client library for Google Analytic v3.
I am able to run the simple app I wrote at home, but when I try at the office it doesn't work. When I run the program I am asked to authorize the php app to my google account. After allowing access I get
Google_IOException: HTTP Error: (0) couldn't connect to host in C:\wamp\www\google\GoogleClientApi\io\Google_CurlIO.php on line 128
It is necessary to connect to a proxy server at my organization. Does anyone know how to use oauth 2 and the php client library to connect to a proxy server.
thanks
Below is the code from my php client.
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.';
回答1:
You have to configure proxy settings in curl. Check Google_CurlIO.php for a line that calls curl_exec($ch)
.
You may need to add something beforehand similar to:
curl_setopt($ch, CURLOPT_PROXY, 'your-proxy-server');
回答2:
Just to add (Since I wasn't able to find any results in google for this) if you want to avoid having to edit the library itself you can specify the additional curl params via the $client object. The code to do so looks roughly like this.
$client = new Google_Client();
$client->getIo()->setOptions(array(
CURLOPT_PROXY => 'myproxy.mywebsite.com',
CURLOPT_PROXYPORT => 8909
));
回答3:
Update for v2.0.0
$client = new Google_Client();
$httpClient = $client->getHttpClient();
$httpClient->setDefaultOption("proxy", "http://{$proxyUser}:{$proxyPass}@{$proxyAddress}:{$proxyPort}");
回答4:
Update for version 2.2.0
The library uses Guzzle which reads the environnement variables to automatically setup (or not) a proxy (see GuzzleHttp\Client class) line 177:
if ($proxy = getenv('HTTPS_PROXY')) {
$defaults['proxy']['https'] = $proxy;
}
I assume you need a HTTPS proxy since Google OAuth won't work over simple HTTP.
Just add
putenv('HTTPS_PROXY=https://{your.proxy.url}:{port_number}');
and it works by itself.
来源:https://stackoverflow.com/questions/17049352/google-api-php-client-library