diff --git a/README.md b/README.md index 0695a79..57fa804 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ includes its own setup instructions. - Java Sync - [.NET/C#](dotnet/hello-world/README.md) - [Node.js](node/hello-world/README.md) -- PHP +- [PHP](php/hello-world/README.md) - [Python](python/hello-world/README.md) - Ruby - Rust diff --git a/php/hello-world/.gitignore b/php/hello-world/.gitignore new file mode 100644 index 0000000..d1502b0 --- /dev/null +++ b/php/hello-world/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock diff --git a/php/hello-world/README.md b/php/hello-world/README.md new file mode 100644 index 0000000..181121b --- /dev/null +++ b/php/hello-world/README.md @@ -0,0 +1,64 @@ +# Get Started with the MongoDB PHP Library + +This sample application connects to a MongoDB deployment, seeds a small +set of sample product documents, and retrieves one of them. Because the +app inserts its own data, you don't need to load an external dataset. + +## Prerequisites + +Before you begin, complete the [Atlas Get Started guide](https://www.mongodb.com/docs/get-started/) +to create a free Atlas deployment and save your database user +credentials. + +You also need the following components installed in your development environment: + +- PHP version 8.2 or later +- Composer version 2.0 or later +- The MongoDB PHP extension (`ext-mongodb`) + +Follow the [Get Started with the PHP Library](https://www.mongodb.com/docs/php-library/current/get-started/) +guide to install the PHP extension for your platform. + +## Installation + +Clone this repository: + +```bash +git clone https://github.com/mongodb/mongodb-code-examples +``` + +Navigate into the `php/hello-world` project directory and install the +MongoDB PHP library with Composer: + +```bash +cd mongodb-code-examples/php/hello-world +composer install +``` + +## Connect to MongoDB + +Set your connection string as an environment variable, replacing +`` with your connection string: + +```bash +export MONGODB_URI="" +``` + +## Run the Application + +```bash +php src/HelloWorld.php +``` + +When you run the app, it inserts a few product documents into the +`get_started.products` collection, then queries and prints one of them: + +``` +{"_id":{"$oid":"..."},"name":"Wireless Mouse","category":"Electronics","price":24.99,"tags":["wireless","usb","ergonomic"]} +``` + +You can run the app more than once. It clears the collection before +each run, so the results stay consistent. + +If you encounter an error or see no output, verify that you set the +`MONGODB_URI` environment variable correctly. diff --git a/php/hello-world/composer.json b/php/hello-world/composer.json new file mode 100644 index 0000000..9ed8095 --- /dev/null +++ b/php/hello-world/composer.json @@ -0,0 +1,10 @@ +{ + "name": "mongodb/hello-world-php", + "description": "Get Started sample application for the MongoDB PHP library", + "type": "project", + "require": { + "php": ">=8.2", + "ext-mongodb": "^2.0", + "mongodb/mongodb": "^2.0" + } +} diff --git a/php/hello-world/src/HelloWorld.php b/php/hello-world/src/HelloWorld.php new file mode 100644 index 0000000..5b18e43 --- /dev/null +++ b/php/hello-world/src/HelloWorld.php @@ -0,0 +1,46 @@ + 'Wireless Mouse', + 'category' => 'Electronics', + 'price' => 24.99, + 'tags' => ['wireless', 'usb', 'ergonomic'], + ], + [ + 'name' => 'Standing Desk', + 'category' => 'Furniture', + 'price' => 349.99, + 'tags' => ['adjustable', 'office'], + ], + [ + 'name' => 'Noise-Cancelling Headphones', + 'category' => 'Electronics', + 'price' => 199.99, + 'tags' => ['bluetooth', 'wireless', 'over-ear'], + ], +]; + +$client = new Client($uri); +$products = $client->get_started->products; + +// Seed the collection so the app has data to query. Clearing the +// collection first keeps results consistent across repeated runs. +$products->deleteMany([]); +$products->insertMany($sampleProducts); + +$product = $products->findOne(['name' => 'Wireless Mouse']); + +echo json_encode($product), "\n";