Bigquery + PHP示例

Nil*_*esh 5 php google-bigquery

有人可以提供使用Bigquery API和PHP的工作示例.我看到有python和java的例子但是找不到PHP的任何东西.

以下是bigquery浏览器https://bigquery.cloud.google.com/?pli=1

例如,您可以在浏览器中运行此SQL

SELECT corpus,count(*) FROM publicdata:samples.shakespeare 
group by corpus limit 5;
Run Code Online (Sandbox Code Playgroud)

我想通过PHP模拟类似的调用.

即使是如何使用PHP API的粗略示例也会有很大帮助.

Mic*_*hri 12

使用适用于PHP的Google API客户端.这是一个执行单个同步查询作业的脚本的简单示例.这使用可下载API客户端中的类名.注意:从SVN中提取的源具有不同的类名.请注意,您必须为客户端密钥,客户端ID,重定向URI和项目ID添加自己的值.

<?php

require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiBigqueryService.php';

session_start();

$client = new apiClient();
// Visit https://developers.google.com/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.

$client->setClientId('XXXXXXXXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXXXXXXXXXXXX');
$client->setRedirectUri('http://www_your_domain.com/somescript.php');

// Your project id
$project_id = 'XXXXXXXXXXXXXXXXXXXX';

// Instantiate a new BigQuery Client 
$bigqueryService = new apiBigqueryService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $client->setAccessToken($client->authenticate());
  $_SESSION['access_token'] = $client->getAccessToken();
}

if (isset($_GET['code'])) {
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
?>
<!doctype html>
<html>
<head>
  <title>BigQuery API Sample</title>
</head>
<body>
<div id='container'>
  <div id='top'><h1>BigQuery API Sample</h1></div>
  <div id='main'>
<?php
  $query = new QueryRequest();
  $query->setQuery('SELECT TOP( title, 10) as title, COUNT(*) as revision_count FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;');

  $jobs = $bigqueryService->jobs;
  $response = $jobs->query($project_id, $query);

  // Do something with the BigQuery API $response data
  print_r($response);

?>
  </div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)