Google Api PHP客户端库 [英] Google Api PHP Client Library

查看:50
本文介绍了Google Api PHP客户端库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google API PHP客户端库用于Google Analytic v3.

I am trying to use the Google API PHP Client library for Google Analytic v3.

我可以运行我在家里编写的简单应用程序,但是当我在办公室尝试时,它不起作用.当我运行该程序时,系统要求将php应用程序授权给我的Google帐户.允许访问后,我得到

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错误:(0)无法在第128行的C:\ wamp \ www \ google \ GoogleClientApi \ io \ Google_CurlIO.php中连接到主机

Google_IOException: HTTP Error: (0) couldn't connect to host in C:\wamp\www\google\GoogleClientApi\io\Google_CurlIO.php on line 128

有必要连接到我组织的代理服务器.有谁知道如何使用oauth 2和php客户端库连接到代理服务器.

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.

谢谢

下面是我的php客户端中的代码.

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

推荐答案

您必须在curl中配置代理设置.在Google_CurlIO.php中检查调用curl_exec($ch)的行.

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,'您的代理服务器');

curl_setopt($ch, CURLOPT_PROXY, 'your-proxy-server');

这篇关于Google Api PHP客户端库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆