googleBigQuery:可捕获的致命错误:参数1传递给Google \ Cloud \ BigQuery \ BigQueryClient :: runQuery()

I am stuck with this error, let me know if there is any workarounds:

googleBigQuery: Catchable fatal error: Argument 1 passed to

Google\Cloud\BigQuery\BigQueryClient::runQuery() must be an instance

of Google\Cloud\BigQuery\JobConfigurationInterface, string given

Code sample:

<?php

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\BigQuery\BigQueryClient;

// get the project ID as the first argument    
$projectId = 'bigquery-public-data';

$bigQuery = new BigQueryClient([
    'projectId' => $projectId,
]);

$query = 'SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words ' .
         'FROM [bigquery-public-data:samples.shakespeare]';

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

if ($queryResults->isComplete()) {
    $i = 0;
    $rows = $queryResults->rows();
    foreach ($rows as $row) {
        printf('--- Row %s ---' . PHP_EOL, ++$i);
        foreach ($row as $column => $value) {
            printf('%s: %s' . PHP_EOL, $column, $value);
        }
    }
    printf('Found %s row(s)' . PHP_EOL, $i);
} else {
    throw new Exception('The query failed to complete');
}
Resolved by using below code..

<?php
# Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

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


// Authenticate using a keyfile path
$cloud = new ServiceBuilder([
    'keyFilePath' => 'keyfile.json'
]);

$bigQuery = $cloud->bigQuery();

//$query = 'SELECT commit FROM `bigquery-public-data.github_repos.commits` WHERE message = ? LIMIT 100';

$queryJobConfig = $bigQuery->query($query)
    ->parameters(['A commit message.']);
$queryResults = $bigQuery->runQuery($queryJobConfig);

I am posting for you a full example as this is much more easier.

You need to setup the service account default credentials see lines with putenv and useApplicationDefaultCredentials(). This is a working code I have using the library https://github.com/googlecloudplatform/google-cloud-php

You need to obtain your service account key file from the console: https://console.cloud.google.com/iam-admin/serviceaccounts/

composer.json

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

php file

# 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);