沃梦达 / 编程问答 / php问题 / 正文

Bigquery + PHP 示例

Bigquery + PHP examples(Bigquery + PHP 示例)

本文介绍了Bigquery + PHP 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

SELECT corpus,count(*) FROM publicdata:samples.shakespeare按语料限制 5 分组;

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

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

解决方案

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

setClientId('XXXXXXXXXXXXXXX.apps.googleusercontent.com');$client->setClientSecret('XXXXXXXXXXXXXXXXXXX');$client->setRedirectUri('http://www_your_domain.com/somescript.php');//你的项目ID$project_id = 'XXXXXXXXXXXXXXXXXXXX';//实例化一个新的 BigQuery 客户端$bigqueryService = new apiBigqueryService($client);如果(isset($_REQUEST ['注销'])){未设置($_SESSION['access_token']);}如果(isset($_SESSION['access_token'])){$client->setAccessToken($_SESSION['access_token']);} 别的 {$client->setAccessToken($client->authenticate());$_SESSION['access_token'] = $client->getAccessToken();}如果(isset($_GET['代码'])){$redirect = 'http://' .$_SERVER['HTTP_HOST'] .$_SERVER['PHP_SELF'];header('位置:' .filter_var($redirect, FILTER_SANITIZE_URL));}?><!doctype html><头><title>BigQuery API 示例</title><身体><div id='容器'><div id='top'><h1>BigQuery API 示例</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);//使用 BigQuery API $response 数据做一些事情打印_r($响应);?>

Can somebody provide working example of using the Bigquery API with PHP. I see there are examples for python and java but could not find anything for PHP.

Here is the bigquery browser https://bigquery.cloud.google.com/?pli=1

For e.g You can run this SQL in the browser

SELECT corpus,count(*) FROM publicdata:samples.shakespeare 
group by corpus limit 5;

I want to simulate similar call via PHP.

Even a rough example of how to use the PHP API will help a lot.

解决方案

Use the Google API Client for PHP. Here's a simple example of a script that does a single synchronous query job. This uses the class names found in the downloadable API client. Note: the source pulled from SVN features different class names. Note where you must add your own values for client secret, client id, redirect URI, and project 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>

这篇关于Bigquery + PHP 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Bigquery + PHP 示例

基础教程推荐