How Do I Connect MongoDB Using PHP Scripts in Google Cloud Functions?

Hi everyone,

I’m currently facing a challenge while integrating MongoDB with a PHP script in Google Cloud Functions. My goal is to run a Google Cloud function that connects to MongoDB using PHP. Although I’ve found numerous resources, they predominantly focus on Node.js implementations, leaving me a bit in the dark with PHP.

Here’s where I’m at:

  • I have been attempting to execute this function on Google Cloud, but I’m encountering a critical error: PHP message: PHP Fatal error: Uncaught Error: Class "MongoDB\Client" not found in /workspace/index.php:13.
    PHP message: PHP Warning: The use statement with non-compound name ‘Exception’ has no effect in /workspace/index.php on line 3
  • This leads me to believe that the main issue lies in installing the MongoDB PHP driver within the Google Cloud function environment.

This is my current code

<?php

use Exception;
use MongoDB\Client;
use Google\CloudFunctions\CloudEvent;

function main(CloudEvent $cloudevent){

  // Replace the placeholder with your Atlas connection string
  $uri =  "";

  // Create a new client and connect to the server
  $client = new MongoDB\Client($uri);
  echo $client;

  try {
      // Send a ping to confirm a successful connection
      $client->selectDatabase('admin')->command(['ping' => 1]);
      echo "Pinged your deployment. You successfully connected to MongoDB!\n";
  } catch (Exception $e) {
      printf($e->getMessage());
  }
}

I’m reaching out for advice on how to properly install and configure the MongoDB PHP driver in Google Cloud Functions. Has anyone here successfully tackled this? Any insights or step-by-step guidance would be incredibly helpful.

Additionally, if you’ve encountered similar issues or have relevant troubleshooting tips, I’d greatly appreciate your input.

Thank you in advance for your help!

[quote=“Ray_Lin, post:1, topic:262288, full:true”]gm-socrates
Hi everyone,

I’m currently facing a challenge while integrating MongoDB with a PHP script in Google Cloud Functions. My goal is to run a Google Cloud function that connects to MongoDB using PHP. Although I’ve found numerous resources, they predominantly focus on Node.js implementations, leaving me a bit in the dark with PHP.

Here’s where I’m at:

  • I have been attempting to execute this function on Google Cloud, but I’m encountering a critical error: PHP message: PHP Fatal error: Uncaught Error: Class "MongoDB\Client" not found in /workspace/index.php:13.
    PHP message: PHP Warning: The use statement with non-compound name ‘Exception’ has no effect in /workspace/index.php on line 3
  • This leads me to believe that the main issue lies in installing the MongoDB PHP driver within the Google Cloud function environment.

This is my current code

<?php

use Exception;
use MongoDB\Client;
use Google\CloudFunctions\CloudEvent;

function main(CloudEvent $cloudevent){

  // Replace the placeholder with your Atlas connection string
  $uri =  "";

  // Create a new client and connect to the server
  $client = new MongoDB\Client($uri);
  echo $client;

  try {
      // Send a ping to confirm a successful connection
      $client->selectDatabase('admin')->command(['ping' => 1]);
      echo "Pinged your deployment. You successfully connected to MongoDB!\n";
  } catch (Exception $e) {
      printf($e->getMessage());
  }
}

I’m reaching out for advice on how to properly install and configure the MongoDB PHP driver in Google Cloud Functions. Has anyone here successfully tackled this? Any insights or step-by-step guidance would be incredibly helpful.

Additionally, if you’ve encountered similar issues or have relevant troubleshooting tips, I’d greatly appreciate your input.

Thank you in advance for your help!
[/quote]

Hello,

Connecting MongoDB using PHP in Google Cloud Functions is a bit different from the more common Node.js implementations, but I can guide you through the process.

The error you’re encountering—PHP Fatal error: Uncaught Error: Class “MongoDB\Client” not found—indicates that the MongoDB PHP driver isn’t installed or properly configured in your Google Cloud Function environment. Let’s address this step by step:

Install the MongoDB PHP Driver: To use the MongoDB PHP driver, you need to install it. Here are a couple of ways to do it:
Using PECL: Run the following commands in your terminal or shell:

pecl channel-update pecl.php.net
pecl install mongodb

Then, add the MongoDB extension to your PHP configuration:

echo "extension=mongodb.so" >> $(php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||")

Using apt-get (if you’re on a Debian-based system):

sudo apt-get update
sudo apt-get install php-pear php5-cli php5-dev
sudo pecl install mongo

Update Your Code: In your existing code, make sure you replace the $uri placeholder with your actual MongoDB Atlas connection string. You can find this connection string in your Atlas dashboard under “Connect” > “Connect your application.”
Verify the Connection: After updating your code and installing the driver, deploy your Google Cloud Function again. Make sure you’ve set up the necessary environment variables (including the connection string). Then, test the function to see if it successfully connects to MongoDB.
Check for Exceptions: If there are any exceptions during the connection attempt, catch them and handle them appropriately. Your current try/catch block looks good, but ensure that the $e->getMessage() output is helpful for debugging.
Remember to replace the placeholder in your $uri with your actual MongoDB Atlas connection string. Once you’ve done that, your Google Cloud Function should be able to connect to MongoDB using PHP.

Hope this work for you.
Best regards,
florence023