0
votes

I am new to GCP. I have created a Bigquery Dataset following this guide.

Now, I am trying to fetch those data with PHP. I have followed this other guide for that.

Here is my code:

<?php

require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\BigQuery\BigQueryClient;
use venor\google\cloud\Core\src\ExponentialBackoff;

 $projectId = 'abcd';
 $query = 'SELECT * FROM `abcd.TestData.BigD2019` LIMIT 1000';

$bigQuery = new BigQueryClient([
    'projectId' => $projectId,
]);
$jobConfig = $bigQuery->query($query);
$job = $bigQuery->startQuery($jobConfig);

$backoff = new ExponentialBackoff(10);
$backoff->execute(function () use ($job) {
    print('Waiting for job to complete' . PHP_EOL);
    $job->reload();
    if (!$job->isComplete()) {
        throw new Exception('Job has not yet completed', 500);
    }
});
$queryResults = $job->queryResults();

$i = 0;
foreach ($queryResults as $row) {
    printf('--- Row %s ---' . PHP_EOL, ++$i);
    foreach ($row as $column => $value) {
        printf('%s: %s' . PHP_EOL, $column, json_encode($value));
    }
}
printf('Found %s row(s)' . PHP_EOL, $i);

?>

While Trying to run this, I am getting below error -

Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message '{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } ' in C:\xampp\htdocs\vendor\google\cloud\Core\src\RequestWrapper.php:336 Stack trace: #0 C:\xampp\htdocs\vendor\google\cloud\Core\src\RequestWrapper.php(189): Google\Cloud\Core\RequestWrapper->convertToGoogleException(Object(GuzzleHttp\Exception\ClientException)) #1 C:\xampp\htdocs\vendor\google\cloud\Core\src\RestTrait.php(96): Google\Cloud\Core\RequestWrapper->send(Object(GuzzleHttp\Psr7\Request), Array) #2 C:\xampp\htdocs\vendor\google\cloud\BigQuery\src\Connection\Rest.php(218): Google\Cloud\BigQuery\Connection\Rest->send('jobs', 'insert', Array) #3 C:\xampp\htdocs\vendor\google\cloud\BigQuery\src\BigQueryClient.php(370): Google\Cloud\BigQuery\Connection\Rest->insertJob(Array) #4 C: in C:\xampp\htdocs\vendor\google\cloud\Core\src\RequestWrapper.php on line 336

I am not able to figure out the exact problem. I am guessing this is possibly due to Authentication.

1
Where are you running this code? I do not see you specifying credentials when you create the client so this means Application Default Credentials (ADC). Add these details to your question. If you are running on Compute Engine, you need to enable the BigQuery APIs in the VM's "access scopes". Shutdown your instance and then edit the VM. Look for the section Identity and API access.John Hanley
I was trying this from localhost. Downloaded Authentication JSON file. Also added as environment variable, but was not working. However, I fixed this by adding one line in PHP - putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\xampp\htdocs\Google\DeployNETAppl-94021a420711.json');Rituparno Bhattacharya

1 Answers

1
votes
use venor\google\cloud\Core\src\ExponentialBackoff;

One thing I noticed is "vendor" written incorrectly.