-
Notifications
You must be signed in to change notification settings - Fork 137
Getting started
The following assumes that Neo4j has already been installed and is accessible via http://localhost:7474 (or your specific hostname)
- Add the following to your
composer.json
file:
{
"require": {
"everyman/neo4jphp": "dev-master"
}
}
- Require in your application:
require("vendor/autoload.php");
- Download neo4jphp.phar
- Move the downloaded file to your include path
- Require in your application:
require("phar://neo4jphp.phar");
Note: this method will be deprecated in the near future. The preferred method is using Composer.
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
- 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");
If PHPUnit is installed, run tests
> ./tests/runtests
Verify that neo4jphp can talk to the Neo4j server
> cd ./examples
> ./bacon.php init
> ./bacon.php path "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);
}
});
You can use the neo4jphp.phar
archive to test your connection to a Neo4j instance from the command line:
> php neo4jphp.phar localhost
Change "localhost" to the host name of your Neo4j instance. If the connection is successful, you will see server and version information.
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.