2
votes

I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:

PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in /xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line 129

The code I have is:

<?php
require 'vendor/autoload.php';

use Google\Cloud\Spanner\SpannerClient;

/* Error start here */
$spanner = new SpannerClient([
    'projectId' => 'my-project-id'
]);

$db = $spanner->connect('instance', 'database');

$userQuery = $db->execute('SELECT * FROM usuario WHERE login = @login', [
    'parameters' => [
        'login' => 'devteam'
    ]
]);

$user = $userQuery->rows()->current();

echo 'Hello ' . $user['login'];

The requirements I use in the composer are:

"require": {
        "google/cloud": "^0.32.1",
        "google/cloud-spanner": "^0.2.2"
}

I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.

3

3 Answers

2
votes

Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:

pecl install grpc 

Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.

1
votes

For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!

0
votes

Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.

E.g. Add extension=grpc.so to your /etc/php/5.6/apache2/php.ini