Skip to content

Getting started

Josh Adell edited this page Feb 15, 2014 · 23 revisions

Installation

The following assumes that Neo4j has already been installed and is accessible via http://localhost:7474 (or your specific hostname)

Quick Setup with Composer

  • Add the following to your composer.json file:
{
  "require": {
    "everyman/neo4jphp": "dev-master"
  }
}
  • Require in your application:
require("vendor/autoload.php");

From Source

Note: Using Composer is the only supported way to include the neo4jphp library.

Get neo4jphp via git checkout git://github.com/jadell/neo4jphp.git or download the latest tagged release from https://github.com/jadell/neo4jphp/archives/master

neo4jphp will need to be put where your application's autoloader can find it.

How this is done depends on how your application is structured, and which framework (if any) you are using. In general, symlink or copy the lib/Everyman directory into your application's autoload path, and set up the autoloader to look for any class in the Everyman namespace under this path. An example of how to do this for CodeIgniter 2 can be found at http://blog.everymansoftware.com/2011/08/getting-neo4jphp-working-with.html

neo4jphp follows the PSR-0 autoloading standard, so any compliant autoloader will work.

If you aren't using a framework, or your framework does not handle autoloading, you can use the following snippet to set up autoloading:

spl_autoload_register(function ($className) {
  if (strpos($className, 'Everyman\Neo4j\\') !== 0) {
    return;
  }
  $libPath = '/path/to/neo4jphp/lib/';  // on Windows: 'C:\path\to\neo4jphp\lib\\';
  $classFile = str_replace('\\',DIRECTORY_SEPARATOR,$className).'.php';
  $classPath = $libPath.$classFile;
  if (file_exists($classPath)) {
      require($classPath);
  }
});

Testing Your Connection

neo4jphp uses a Client object to encapsulate all communication with the server. This client is used in the construction of many of neo4jphp's other objects (nodes, relationships, etc.) The following code demonstrates how to create a Client and test that it can connect to your Neo4j instance.

Create a script named neo4jphp_connect_test.php:

<?php
require('vendor/autoload.php'); // or your custom autoloader

// Connecting to the default port 7474 on localhost
$client = new Everyman\Neo4j\Client();

// Connecting to a different port or host
$client = new Everyman\Neo4j\Client('host.example.com', 7575);

// Connecting using HTTPS and Basic Auth
$client = new Everyman\Neo4j\Client();
$client->getTransport()
  ->useHttps()
  ->setAuth('username', 'password');

// Test connection to server
print_r($client->getServerInfo());

Execute the script:

> php neo4jphp_connect_test.php

If you see your server's information, then you have successfully connected!

Congratulations! neo4jphp is set up and ready to use in your application.

Clone this wiki locally