If you’re looking to use Neo4J with PHP, then there’s a new library which has been recently released. I used to work on PHP back in my college days. I still do some work in PHP – mostly in WordPress. So, I decided to check it out.
1. Install PHP
The first thing that you need to do is install PHP. I use Ubuntu, so it’s pretty straightforward. You can check out this post here.
Make sure PHP is running before moving on to Neo4J.
2. Install Neo4J on Docker
The easiest way to get started with Neo4J is to use a docker image. First, pull the docker image.
$ docker pull neo4j
Once the image has been pulled, run the container.
$ docker run \ --publish=7474:7474 --publish=7687:7687 \ --volume=$HOME/neo4j/data:/data \ neo4j
You can verify if the container is running using the below command.
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 74cc32ce176d neo4j "/sbin/tini -g -- /d…" About an hour ago Up About an hour 0.0.0.0:7474->7474/tcp, :::7474->7474/tcp, 7473/tcp, 0.0.0.0:7687->7687/tcp, :::7687->7687/tcp nostalgic_hodgkin
Once the container is up, you should be able to open the Neo4J console on http://localhost:7474.
3. Use Composer to setup drivers and clients
To setup composer on Ubuntu, you can use the below command.
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" $ php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" $ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer $ php -r "unlink('composer-setup.php');"
You can use composer to setup the drivers and clients.
$ composer require laudis/neo4j-php-client $ composer require nyholm/psr7 nyholm/psr7-server kriswallsmith/buzz
Once the above steps are done, you can get started with setting up the PHP client.
use \Laudis\Neo4j\DriverFactory; use \Laudis\Neo4j\Authentication\Authenticate; $auth = Authenticate::basic('neo4j', 'test'); $boltDriver = DriverFactory::create('bolt://neo4j:test@localhost'); $neo4jDriver = DriverFactory::create('neo4j://core1:7777', null, $auth); $httpDriver = DriverFactory::create('https://localhost:7473');
You can check out the more detailed post on the Neo4J site.
You can also check out the GitHub repo.
So, that’s it for this article. Thank you for reading and happy coding.