sergio campo
sergio campo

Reputation: 276

500 (Internal Server Error) Hosting Web in Google Cloud - Compute Engine VM + Firestore + PHP + Apache2

The implementation of Web on Google Compute Engine VM (free trial) to connect to a firestore database has the following files:

index.html:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
  <div class="result"></div>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
      function create () {
        $.ajax({
            url: 'firestoreData.php',
            type: 'post',
            dataType: 'json',
            data: { funct: "getData", name:"testArgument"},
            success: function(response) { 
                    console.log(response.result);              
            },
            error: function(jqxhr, status, exception) {
                console.log("error:" + exception);
            }
        
        });
      }
    </script>
    <button type="button" onclick="create()">Click Me</button>
 </body>
</html>

firestoreData.php:

<?php
namespace Google\Cloud\Samples\Firestore;
require_once __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Firestore\FirestoreClient;
function getData($name)
{
  // Create the Cloud Firestore client
  $db = new FirestoreClient([
    'projectId' => 'project-id',
    'credentials' => 'project-XXX.json'
  ]);
  return json_encode(array('result'=>'successfuly registered'));
}
if ($_POST['funct']=="getData") { 
  echo getData($_POST['name']);
}

I got the error in the Chrome Explorer " 500 (Internal Server Error)". Please see the image: Capture Chrome

If I remove the line in firestoreData.php there is no problem.

$db = new FirestoreClient([
        'projectId' => 'project-id',
        'credentials' => 'project-XXX.json'
      ]); 

and if I check the firestoreData.php file in the VM console with the command:

php firestoreData.php

there is no problem. How can I resolve this problem?

Upvotes: 0

Views: 266

Answers (1)

sergio campo
sergio campo

Reputation: 276

Fixed the error adding extension=grpc.so in /etc/php/7.4/apache2/php.ini. It should be also included in /etc/php/7.4/cli/php.ini.

Upvotes: 1

Related Questions