title: Fetching data from Bigquery with PHP
body: <p>I am new to GCP. I have created a Bigquery Dataset following <a href="https://cloud.google.com/bigquery/docs/quickstarts/quickstart-web-ui" rel="nofollow noreferrer">this guide</a>.</p>
  
  <p>Now, I am trying to fetch those data with PHP. I have followed <a href="https://cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries" rel="nofollow noreferrer">this other guide</a> for that.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?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' =&gt; $projectId,
  ]);
  $jobConfig = $bigQuery-&gt;query($query);
  $job = $bigQuery-&gt;startQuery($jobConfig);
  
  $backoff = new ExponentialBackoff(10);
  $backoff-&gt;execute(function () use ($job) {
      print('Waiting for job to complete' . PHP_EOL);
      $job-&gt;reload();
      if (!$job-&gt;isComplete()) {
          throw new Exception('Job has not yet completed', 500);
      }
  });
  $queryResults = $job-&gt;queryResults();
  
  $i = 0;
  foreach ($queryResults as $row) {
      printf('--- Row %s ---' . PHP_EOL, ++$i);
      foreach ($row as $column =&gt; $value) {
          printf('%s: %s' . PHP_EOL, $column, json_encode($value));
      }
  }
  printf('Found %s row(s)' . PHP_EOL, $i);
  
  ?&gt;
  </code></pre>
  
  <p>While Trying to run this, I am getting below error - </p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>I am not able to figure out the exact problem. I am guessing this is possibly  due to Authentication.</p>