11<?php
2- /*
3- require 'vendor/autoload.php';
42
5- use Laudis\Neo4j\ClientBuilder;
6- use Laudis\Neo4j\Authentication\Authenticate;
7-
8- $address = 'neo4j+s://bb79fe35.databases.neo4j.io';
3+ $ neo4j_url = 'https://f2455ee6.databases.neo4j.io ' ;
94$ username = 'neo4j ' ;
10- $password = 'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0';
5+ $ password = 'h5YLhuoSnPD6yMy8OwmFPXs6WkL8uX25zxHCKhiF_hY ' ;
6+
7+ function runNeo4jTransaction ($ query ): void
8+ {
9+ global $ neo4j_url , $ username , $ password ;
10+
11+ $ ch = curl_init ();
12+
13+ echo "Authorization Header: " . base64_encode ("$ username: $ password " ) . "\n" ;
14+
15+ curl_setopt ($ ch , CURLOPT_URL , $ neo4j_url . '/db/neo4j/transaction/commit ' );
16+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
17+ curl_setopt ($ ch , CURLOPT_POST , true );
18+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
19+ 'Content-Type: application/json ' ,
20+ 'Authorization: Basic ' . base64_encode ("$ username: $ password " )
21+ ]);
22+
23+ $ payload = json_encode ([
24+ "statements " => []
25+ ]);
26+
27+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ payload );
28+
29+ $ response = curl_exec ($ ch );
30+ if (curl_errno ($ ch )) {
31+ echo 'Error starting transaction: ' . curl_error ($ ch );
32+ return ;
33+ }
34+
35+ echo "Transaction Start Response: " ;
36+ print_r ($ response );
37+
38+ $ http_status = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
39+ if ($ http_status === 403 ) {
40+ echo "403 Forbidden: Check credentials and permissions. \n" ;
41+ }
42+
43+ $ transaction_data = json_decode ($ response , true );
44+
45+ if (!isset ($ transaction_data ['results ' ])) {
46+ echo "Transaction creation failed or missing results. Response: " ;
47+ print_r ($ transaction_data );
48+ return ;
49+ }
50+
51+ $ query_data = [
52+ "statements " => [
53+ [
54+ "statement " => $ query
55+ ]
56+ ]
57+ ];
58+
59+ curl_setopt ($ ch , CURLOPT_URL , $ neo4j_url . '/db/neo4j/query/v2/tx ' );
60+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ query_data ));
61+
62+ $ response = curl_exec ($ ch );
63+ if (curl_errno ($ ch )) {
64+ echo 'Error running query: ' . curl_error ($ ch );
65+ return ;
66+ }
1167
68+ $ commit_data = json_decode ($ response , true );
69+ if (isset ($ commit_data ['errors ' ]) && count ($ commit_data ['errors ' ]) > 0 ) {
70+ echo "Query error: " . $ commit_data ['errors ' ][0 ]['message ' ];
71+ return ;
72+ }
1273
13- // Create a Neo4j client using the Laudis PHP driver with authentication
14- $client = ClientBuilder::create()
15- ->withDriver(
16- 'bolt',
17- $address,
18- Authenticate::basic($username, $password) // Proper authentication object
19- )
20- ->build();
74+ echo "Transaction successful. Data returned: " ;
75+ print_r ($ commit_data );
2176
22- // Define the Cypher query
23- $cypherQuery = 'MATCH (n:Person) RETURN n LIMIT 10';
77+ curl_close ( $ ch );
78+ }
2479
25- // Run the query and fetch results
26- $results = $client->run($cypherQuery);
80+ $ query = 'MATCH (n) RETURN n LIMIT 5 ' ;
2781
28- // Print the results
29- echo "<pre>"; // Optional: formats the output nicely for readability
30- print_r($results->toArray());
31- echo "</pre>";
32- */
82+ runNeo4jTransaction ($ query );
0 commit comments