PHP
Scenarios
DDS allows you to perform data operations using PHP. You can connect to a DB instance through an SSL connection or a non-SSL connection. SSL connections provide stronger security.
By default, SSL is disabled for new DDS instances. For details about how to enable SSL, see Configuring SSL.
This section describes how to connect to a cluster instance using PHP.
Prerequisites
- Ensure that the ECS can communicate with the DDS instance. To test the connectivity to the instance's IP address and port, run the curl command:
curl ip:port
If the message "It looks like you are trying to access MongoDB over HTTP on the native driver port." is displayed, the ECS and DDS instance can communicate with each other.
- If SSL is enabled, you need to download the root certificate and upload it to the ECS.
Obtaining and Using the Driver
For PHP information, visit https://www.php.net/mongodb-driver-manager.construct.
Enabling and Disabling SSL
- Run MongoDB\Client::__construct() to create a client instance.
function __construct( ?string $uri = null, array $uriOptions = [], array $driverOptions = [] )
- Use $uriOptions to set SSL to true to enable the SSL connection. Use $driverOptions to set ca_file to the CA certificate path and allow_invalid_hostname to true.
<?php require 'vendor/autoload.php'; // include Composer goodies $replicaset_url = 'mongodb://rwuser:*****@192.168.***.***:8635,192.168.***.***:8635/test?authSource=admin'; $test_db = 'test_db'; $test_coll = 'test_coll'; //Create mongoclient. $client = new MongoDB\Client( ....$replicaset_url, [ 'ssl' => true, ], [ "ca_file" => "/path/to/ca.pem", "allow_invalid_hostname" => true ] ); $collection = $client->$test_db->$test_coll; //Insert a record. $result = $collection->insertOne([ 'username' => 'admin', 'email' => 'admin@example.com', ]); echo "Object ID: '{$result->getInsertedId()}'", "\n"; //Query a record. $result = $collection->find(['username' => 'admin']); foreach ($result as $entry) { echo $entry->_id, ': ', $entry->email, "\n"; } ?>
<?php
require 'vendor/autoload.php'; // include Composer goodies
$replicaset_url = 'mongodb://rwuser:*****@192.168.***.***:8635,192.168.***.***:8635/test?authSource=admin';
$test_db = 'test_db';
$test_coll = 'test_coll';
//Create mongoclient.
$client = new MongoDB\Client($replicaset_url);
$collection = $client->$test_db->$test_coll;
//Insert a record.
$result = $collection->insertOne([
'username' => 'admin',
'email' => 'admin@example.com',
]);
echo "Object ID: '{$result->getInsertedId()}'", "\n";
//Query a record.
$result = $collection->find(['username' => 'admin']);
foreach ($result as $entry) {
echo $entry->_id, ': ', $entry->email, "\n";
}
?>
- The authentication database in the URL must be admin. That means setting authSource to admin.
- Change the authentication database of the rwuser user to admin, and then switch to the service database after authentication.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot