Skip to content

Getting started

jadell edited this page Jan 21, 2012 · 23 revisions

Installation

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

Quick Setup

  • Download neo4jphp.phar
  • Move the downloaded file to your include path
  • Require in your application:
require("phar://neo4jphp.phar");

From Source

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

Change into the Neo4jPHP directory

cd neo4jphp

With Phing

  • Build the package
phing test
phing package
  • Move the package file neo4jphp.phar to your include path
  • Require in your application: require("phar://neo4jphp.phar");

Without Phing

If PHPUnit is installed, run tests

./tests/runtests

Verify that Neo4jPHP can talk to the Neo4j server

cd ./examples  
./bacon.php init  
./bacon.php "Keanu Reeves" "Kevin Bacon"  

You should see output indicating a path has been found between the two actors.

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) {
  $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

You can use the neo4jphp.phar archive to test your connection to a Neo4j instance from the command line:

> php neo4j.phar localhost

Change "localhost" to the host name of your Neo4j instance. If the connection is successful, you will see server and version information.

Creating a Connection to Neo4j in Your PHP Application

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 creates a Client:

// 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');

Check the connection the server:

print_r($client->getServerInfo());

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

Clone this wiki locally