Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
pull_request:
paths:
- '**/*.php'
workflow_dispatch:

jobs:
php-cs-fixer:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.ref }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#IDE
.idea/

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"guzzlehttp/guzzle": "^7.9",
"psr/http-client": "^1.0",
"ext-json": "*",
"php": "^8.1"
"php": "^8.1",
"nyholm/psr7": "^1.8"
},
"require-dev": {
"phpunit/phpunit": "^11.0",
Expand Down Expand Up @@ -39,4 +40,4 @@
"cs:fix": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
}

}
}
80 changes: 79 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions src/BasicAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
namespace Neo4j\QueryAPI;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class BasicAuthentication implements AuthenticateInterface
{
public function __construct(private string $username, private string $password)
private string $username;
private string $password;

public function __construct(?string $username = null, ?string $password = null)
{
$this->username = $username;
$this->password = $password;
// Use provided values or fallback to environment variables
$this->username = $username ?? getenv("NEO4J_USERNAME") ?: '';
$this->password = $password ?? getenv("NEO4J_PASSWORD") ?: '';
}

public function authenticate(RequestInterface $request): RequestInterface
{
$authHeader = 'Basic ' . base64_encode($this->username . ':' . $this->password);
$authHeader = $this->getHeader();
return $request->withHeader('Authorization', $authHeader);
}

public function getHeader(): string
{
return 'Basic ' . base64_encode($this->username . ':' . $this->password);
Expand All @@ -27,5 +31,4 @@ public function getType(): string
{
return 'Basic';
}

}
1 change: 1 addition & 0 deletions src/BearerAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function authenticate(RequestInterface $request): RequestInterface
$authHeader = 'Bearer ' . $this->token;
return $request->withHeader('Authorization', $authHeader);
}

public function getHeader(): string
{
return 'Bearer ' . $this->token;
Expand Down
108 changes: 108 additions & 0 deletions src/Neo4jPhp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

require __DIR__.'/../vendor/autoload.php'; // Assuming you have Composer installed and dependencies loaded

use Neo4j\QueryAPI\Neo4jQueryAPI;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Objects\Bookmarks;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use stdClass;

try {
// Step 1: Initialize the connection to Neo4j with login credentials (authentication)
$neo4jAddress = 'https://6f72daa1.databases.neo4j.io'; // Replace with your Neo4j database address
$username = 'neo4j';
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';

// Base64 encode the username and password for Basic Authentication
$credentials = base64_encode("$username:$password");

// Set up the authentication header with base64-encoded credentials
$headers = [
'Authorization' => 'Basic ' . $credentials,
'Content-Type' => 'application/json',
];

// Initialize the client with the authorization header
$client = new \GuzzleHttp\Client([
'base_uri' => rtrim($neo4jAddress, '/'),
'timeout' => 10.0,
'headers' => $headers,
]);

// Step 2: Create the Cypher query
$cypherQuery = 'MATCH (n) RETURN n LIMIT 10';
$parameters = []; // No parameters in this query
$database = 'neo4j'; // Default Neo4j database

echo "Running Cypher Query: $cypherQuery\n";

// Prepare the payload for the Cypher query
$payload = [
'statement' => $cypherQuery,
'parameters' => new stdClass(), // No parameters
'includeCounters' => true,
];

// Step 3: Send the request to Neo4j
$response = $client->post("/db/{$database}/query/v2", [
'json' => $payload,
]);

// Parse the response body as JSON
$responseData = json_decode($response->getBody()->getContents(), true);

// Check for errors in the response
if (isset($responseData['errors']) && count($responseData['errors']) > 0) {
echo "Error: " . $responseData['errors'][0]['message'] . "\n";
} else {
// Step 4: Output the result of the query
echo "Query Results:\n";
foreach ($responseData['data'] as $row) {
print_r($row); // Print each row's data
}
}

// Step 5: Begin a new transaction
$transactionResponse = $client->post("/db/neo4j/query/v2/tx");
$transactionData = json_decode($transactionResponse->getBody()->getContents(), true);
$transactionId = $transactionData['transaction']['id']; // Retrieve the transaction ID

echo "Transaction started successfully.\n";
echo "Transaction ID: $transactionId\n";

// You can also fetch additional transaction details if available in the response
// Example: transaction metadata or counters
if (isset($transactionData['transaction']['metadata'])) {
echo "Transaction Metadata: \n";
print_r($transactionData['transaction']['metadata']);
}

// Step 6: Execute a query within the transaction
$cypherTransactionQuery = 'MATCH (n) SET n.modified = true RETURN n LIMIT 5';
$transactionPayload = [
'statement' => $cypherTransactionQuery,
'parameters' => new stdClass(), // No parameters
];

// Execute the transaction query
$transactionQueryResponse = $client->post("/db/neo4j/query/v2/tx/{$transactionId}/commit", [
'json' => $transactionPayload,
]);

$transactionQueryData = json_decode($transactionQueryResponse->getBody()->getContents(), true);

// Check for any errors in the transaction query
if (isset($transactionQueryData['errors']) && count($transactionQueryData['errors']) > 0) {
echo "Transaction Error: " . $transactionQueryData['errors'][0]['message'] . "\n";
} else {
echo "Transaction Query Results:\n";
print_r($transactionQueryData['data']); // Print transaction results
}

} catch (RequestException $e) {
echo "Request Error: " . $e->getMessage() . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Loading