Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions php/hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
64 changes: 64 additions & 0 deletions php/hello-world/README.md
Original file line number Diff line number Diff line change
@@ -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
`<connection string uri>` with your connection string:

```bash
export MONGODB_URI="<connection string 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.
10 changes: 10 additions & 0 deletions php/hello-world/composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
46 changes: 46 additions & 0 deletions php/hello-world/src/HelloWorld.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

use MongoDB\Client;

$uri = getenv('MONGODB_URI');
if ($uri === false || $uri === '') {
fwrite(STDERR, "Set the MONGODB_URI environment variable to your connection string\n");
exit(1);
}

// A few sample product documents seeded by this app so you can run
// it without loading an external dataset.
$sampleProducts = [
[
'name' => '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";