PHP client guide

Learn how to create a PHP application that connects to the Memgraph database and executes simple queries.

Community-contributed library PHP Bolt (opens in a new tab) by Michal Štefaňák is the most commonly used to connect to Memgraph with PHP via Bolt protocol. Memgraph and Neo4j both support Bolt protocol and Cypher queries, which means that same client can be used to connect to both databases. This is very convenient if switching between the two databases is needed.


The following guide will demonstrate how to start Memgraph, connect to it, seed the database with data, and run simple read and write queries with the PHP Bolt client.

The necessary prerequisites that should be installed in your local environment are:

Run Memgraph

Start by running Memgraph in the background. The easiest way to install and run Memgraph is by using Docker. Start the container with the following command:

docker run -p 7687:7687 -p 7444:7444 -p 3000:3000 --name memgraph memgraph/memgraph-platform

The command above will start Memgraph Platform, which includes Memgraph database, Memgraph Lab and Memgraph MAGE. Memgraph uses Bolt protocol to communicate with the client using the exposed 7687 port. Memgraph Lab is a web application you can use to visualize the data. It's accessible at http://localhost:3000 (opens in a new tab) if Memgraph Platform is running correctly. The 7444 port enables Memgraph Lab to access and preview the logs, which is why both of these ports need to be exposed.

For more information, visit the getting started guide on how to run Memgraph with Docker.

Create a directory

Next, create a new directory for your application, for example hello-memgraph and position yourself in it.

mkdir hello-memgraph
cd hello-memgraph

Create PHP script

Create index.php file and add the following code to it:

// Create a connection class and specify target host and port, default is localhost.
$conn = new \Bolt\connection\Socket();
// Create a new Bolt instance and provide connection object.
$bolt = new \Bolt\Bolt($conn);
// Set available Bolt versions for Memgraph.
// Build and get protocol version instance which creates connection and executes handshake.
$protocol = $bolt->build();
// Login to database with credentials.
$protocol->logon(\Bolt\helpers\Auth::basic('', ''));
// Pipeline two messages. One to execute query with parameters and second to pull records.
    ->run('CREATE (a:Greeting) SET a.message = $message RETURN id(a) AS nodeId, a.message AS message', ['message' => 'Hello, World!'])
// Server responses are waiting to be fetched through iterator.
$rows = iterator_to_array($protocol->getResponses(), false);
// Get content from requested record.
$row = $rows[1]->getContent();
echo 'Node ' . $row[0] . ' says: ' . $row[1];

In the example above Bolt protocol version 5.2 is used. When you're connecting to Memgraph with lower versions of Bolt protocol, use arguments for authentication in the hello() procedure, instead of using logon().

$bolt = new \Bolt\Bolt($conn);
$bolt->setProtocolVersions(4.3, 4.1, 4.2);
$protocol->hello(\Bolt\helpers\Auth::basic('', ''));

Install the PHP library

Make sure you are located in the hello-memgraph folder in the terminal. Then, run a composer command to get the required library:

composer require stefanak-michal/bolt

That command will create composer.json file.

To use Bolt protocol version 5.2 with Memgraph, install PHP Bolt library version >= 6.1.3.

Run the application

To run the application, run the following command in terminal:

php -S localhost:4000

Open you browser, enter localhost:4000 as URL and you should see an output similar to the following:

Node 0 says: Hello, World!

Another way of running your application is by adding a script in the composer.json file:

    "require": {
        "stefanak-michal/bolt": "6.1.3"
    "scripts": {
        "start": "php -S localhost:4000"
    "config": {
        "allow-plugins": {
            "php-http/discovery": true

With the above update of the composer.json file, it is enough to run composer run start to run the application.

Visualize data

To visualize objects created in the database with the index.php script, head over to http://localhost:3000/ (opens in a new tab) and run MATCH (n) RETURN n; in the Query Execution tab. That query will visualize the created node. By clicking on a node, you can explore different properties.


Next steps

This guide makes a good starting point when building PHP applications. Check out the PHP Bolt driver repository (opens in a new tab) to learn more about using the PHP Bolt library.

You can simplify the usage of this library with the Memgraph Bolt wrapper (opens in a new tab).

If you encounter serialization errors while using PHP client, we recommend referring to our Serialization errors page for detailed guidance on troubleshooting and best practices.