Vinoth Sabanadesan Vinoth Sabanadesan - 3 years ago 236
PHP Question

Google Api PHP Client Library

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.';

Answer Source

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');

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download