【问题标题】:PHP and Google API (BigQuery to be Specific) [duplicate]PHP 和 Google API(具体为 BigQuery)[重复]
【发布时间】:2014-10-15 20:38:07
【问题描述】:

我目前正在尝试开发某种 Web 应用程序,我需要它能够对具有超过 50,000,000 行的表执行查询。 我考虑过使用 Google BigQuery 来解决这个问题,但我在使用他们的 API 时遇到了问题。 在过去的 3 个小时里,我在整个互联网上进行了搜索,但找不到一种方法来获得身份验证并使用 PHP only 执行查询。对于身份验证,我找到了一种使用 javascript 的方法,但这对我不利。我需要它只是服务器端。 整个 php google api 文档对我来说似乎已经过时了。 非常感谢,Sagi。

【问题讨论】:

标签: php google-bigquery google-api-php-client


【解决方案1】:

这是一个适用于我们的示例代码:

session_start();

define('PROJECT_ID', 'edited');
define('DATASET_ID', 'edited');
define('API_KEY', 'edited');

$client_id = 'edited';
$service_account_name = 'edited';
$key_file_location = '.ssh/privatekey-bigquery.p12';
$service_token_file_location = 'bigquery_current_service_token.json';

set_include_path("google-api-php/src/" . PATH_SEPARATOR . get_include_path());
require_once 'google-api-php/src/Google/Client.php';
require_once 'google-api-php/src/Google/Service/Bigquery.php';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
//$client->setDeveloperKey(API_KEY);

if (!is_file($service_token_file_location)) {
    if (!is_writable($service_token_file_location)) {
        @chmod($service_token_file_location, 0777);
        if (!is_writable($service_token_file_location)) {
            die('Service token file is not writable: ' . $service_token_file_location);
        }
    }
    file_put_contents($service_token_file_location, '');
} else {
    if (!is_writable($service_token_file_location)) {
        @chmod($service_token_file_location, 0777);
        if (!is_writable($service_token_file_location)) {
            die('Service token file is not writable: ' . $service_token_file_location);
        }
    }
}
$service_token = @file_get_contents($service_token_file_location);
if (!empty($service_token)) {
    $client->setAccessToken($service_token);
}
if (!file_exists($key_file_location)) {
    die('Key file is missing: ' . $key_file_location);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
        $service_account_name, array(
    'https://www.googleapis.com/auth/bigquery',
        ), $key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}
$service_token = $client->getAccessToken();
file_put_contents($service_token_file_location, $service_token);

// start using $client

【讨论】:

  • 嗨,伙计,感谢您的回复。请问变量$service_token_file_location是什么意思?我需要自己创建文件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多