Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BigQuery PHP对未定义方法Google_Client::setAssertionCredentials()的调用_Php_Api_Google Bigquery - Fatal编程技术网

BigQuery PHP对未定义方法Google_Client::setAssertionCredentials()的调用

BigQuery PHP对未定义方法Google_Client::setAssertionCredentials()的调用,php,api,google-bigquery,Php,Api,Google Bigquery,所以基本上我试着从第二个答案开始,但它有一个错误 致命错误:调用未定义的方法 中的Google_客户端::setAssertionCredentials() 这是我的密码 require_once 'xxx/vendor/autoload.php'; public function createGClient() { define("CLIENT_ID", "xxx.apps.googleusercontent.com"); define("SERVICE_ACCOUNT_NAM

所以基本上我试着从第二个答案开始,但它有一个错误

致命错误:调用未定义的方法 中的Google_客户端::setAssertionCredentials()

这是我的密码

require_once 'xxx/vendor/autoload.php';
public function createGClient() {
    define("CLIENT_ID", "xxx.apps.googleusercontent.com");
    define("SERVICE_ACCOUNT_NAME","xxx@xxx.ccc");
    define("KEY_FILE",'xxx');

    define("PROJECT_ID","xxx");
    define("DATASET_ID","xxx");
    define("TABLE_ID","xxx");

    $this->client = new Google_Client();
    $this->client->setApplicationName("Test");  

    $key = file_get_contents(KEY_FILE);
    $this->client->setAssertionCredentials(
    Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/bigquery'), $key, "notasecret"));
    $this->client->setClientId(CLIENT_ID);
    $this->service = new Google_Service_Bigquery($this->client);
}

public function runQuery() {
    // To see the a list of tables  
    print_r($this->service->tables->listTables(PROJECT_ID, DATASET_ID));

    // To see details of a table
    print_r($this->service->tables->get(PROJECT_ID, DATASET_ID, TABLE_ID));

    // To query a table
    $jobs = $this->service->jobs;
    $query = new Google_Service_Bigquery_QueryRequest();
    $query->setQuery("SELECT * FROM wherever;");
    $response = $jobs->query(PROJECT_ID, $query);
    print_r($response);
}

我已经安装了指南/文档中的所有内容。有人能帮我吗,因为我什么都试过了,但它不起作用,非常感谢。

不再使用该版本的PHP库,因为它已经过时并且缺少文档

下面链接了一个较新的,它与设置服务帐户默认凭据的需要一起工作,请参见带有
putenv
useApplicationDefaultCredentials()
的行。这是我使用库获得的工作代码,您需要从控制台获取服务帐户密钥文件:

composer.json

{
    "require": {
        "google/cloud": "^0.13.0",
        "google/apiclient": "^2.0"
    }
}
php文件

# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\ServiceBuilder;

$query="SELECT repository_url, 
       repository_has_downloads 
FROM   [publicdata:samples.github_timeline]
LIMIT  10";
$client = new Google_Client();
putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__) . '/.ssh/dummyname-7f0004z148e1.json');//this can be created with other ENV mode server side
$client->useApplicationDefaultCredentials();

$builder = new ServiceBuilder([
                'projectId' => 'edited',
        ]);

        $bigQuery = $builder->bigQuery();

        $job = $bigQuery->runQueryAsJob($query);
        $info=$job->info();
//      print_r($info);
//      exit;
        $queryResults = $job->queryResults();

        /*$queryResults = $bigQuery->runQuery(
            $query,
            ['useLegacySql' => true]);*/

        if ($queryResults->isComplete()) 
        {
            $i = 0;
            $rows = $queryResults->rows();

            foreach ($rows as $row) 
            {
                $i++;

                $result[$i] = $row;
            }
        } 
        else 
        {
            throw new Exception('The query failed to complete');
        }

        print_r($result);

“致命错误:在”中未找到类“Google\Cloud\ServiceBuilder”,请确保两个编写器库都已正确安装。当然,您需要导入
vendor/autoload.php
,composer才能正常工作。非常感谢,我重新安装了所有东西,它工作得非常好。
默认凭证
不应该意味着它使用自动生成的GAE和GCE吗?因此,无需在
GOOGLE\u APPLICATION\u CREDENTIALS
中生成一个并指定为密钥文件?您需要使用一个并指定它可以是面板中的第一个。默认情况下,PHP库意味着,如果作为ENV变量加载到服务器上,它将尝试找出它。但是,如果我们处理多个项目,通常会在lib中指定每个项目。