|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__.'/../vendor/autoload.php'; // Assuming you have Composer installed and dependencies loaded |
| 4 | + |
| 5 | +use Neo4j\QueryAPI\Neo4jQueryAPI; |
| 6 | +use Neo4j\QueryAPI\Objects\Authentication; |
| 7 | +use Neo4j\QueryAPI\Objects\Bookmarks; |
| 8 | +use GuzzleHttp\Client; |
| 9 | +use GuzzleHttp\Exception\RequestException; |
| 10 | +use stdClass; |
| 11 | + |
| 12 | +try { |
| 13 | + // Step 1: Initialize the connection to Neo4j with login credentials (authentication) |
| 14 | + $neo4jAddress = 'https://6f72daa1.databases.neo4j.io'; // Replace with your Neo4j database address |
| 15 | + $username = 'neo4j'; |
| 16 | + $password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0'; |
| 17 | + |
| 18 | + // Base64 encode the username and password for Basic Authentication |
| 19 | + $credentials = base64_encode("$username:$password"); |
| 20 | + |
| 21 | + // Set up the authentication header with base64-encoded credentials |
| 22 | + $headers = [ |
| 23 | + 'Authorization' => 'Basic ' . $credentials, |
| 24 | + 'Content-Type' => 'application/json', |
| 25 | + ]; |
| 26 | + |
| 27 | + // Initialize the client with the authorization header |
| 28 | + $client = new \GuzzleHttp\Client([ |
| 29 | + 'base_uri' => rtrim($neo4jAddress, '/'), |
| 30 | + 'timeout' => 10.0, |
| 31 | + 'headers' => $headers, |
| 32 | + ]); |
| 33 | + |
| 34 | + // Step 2: Create the Cypher query |
| 35 | + $cypherQuery = 'MATCH (n) RETURN n LIMIT 10'; |
| 36 | + $parameters = []; // No parameters in this query |
| 37 | + $database = 'neo4j'; // Default Neo4j database |
| 38 | + |
| 39 | + echo "Running Cypher Query: $cypherQuery\n"; |
| 40 | + |
| 41 | + // Prepare the payload for the Cypher query |
| 42 | + $payload = [ |
| 43 | + 'statement' => $cypherQuery, |
| 44 | + 'parameters' => new stdClass(), // No parameters |
| 45 | + 'includeCounters' => true, |
| 46 | + ]; |
| 47 | + |
| 48 | + // Step 3: Send the request to Neo4j |
| 49 | + $response = $client->post("/db/{$database}/query/v2", [ |
| 50 | + 'json' => $payload, |
| 51 | + ]); |
| 52 | + |
| 53 | + // Parse the response body as JSON |
| 54 | + $responseData = json_decode($response->getBody()->getContents(), true); |
| 55 | + |
| 56 | + // Check for errors in the response |
| 57 | + if (isset($responseData['errors']) && count($responseData['errors']) > 0) { |
| 58 | + echo "Error: " . $responseData['errors'][0]['message'] . "\n"; |
| 59 | + } else { |
| 60 | + // Step 4: Output the result of the query |
| 61 | + echo "Query Results:\n"; |
| 62 | + foreach ($responseData['data'] as $row) { |
| 63 | + print_r($row); // Print each row's data |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Step 5: Begin a new transaction |
| 68 | + $transactionResponse = $client->post("/db/neo4j/query/v2/tx"); |
| 69 | + $transactionData = json_decode($transactionResponse->getBody()->getContents(), true); |
| 70 | + $transactionId = $transactionData['transaction']['id']; // Retrieve the transaction ID |
| 71 | + |
| 72 | + echo "Transaction started successfully.\n"; |
| 73 | + echo "Transaction ID: $transactionId\n"; |
| 74 | + |
| 75 | + // You can also fetch additional transaction details if available in the response |
| 76 | + // Example: transaction metadata or counters |
| 77 | + if (isset($transactionData['transaction']['metadata'])) { |
| 78 | + echo "Transaction Metadata: \n"; |
| 79 | + print_r($transactionData['transaction']['metadata']); |
| 80 | + } |
| 81 | + |
| 82 | + // Step 6: Execute a query within the transaction |
| 83 | + $cypherTransactionQuery = 'MATCH (n) SET n.modified = true RETURN n LIMIT 5'; |
| 84 | + $transactionPayload = [ |
| 85 | + 'statement' => $cypherTransactionQuery, |
| 86 | + 'parameters' => new stdClass(), // No parameters |
| 87 | + ]; |
| 88 | + |
| 89 | + // Execute the transaction query |
| 90 | + $transactionQueryResponse = $client->post("/db/neo4j/query/v2/tx/{$transactionId}/commit", [ |
| 91 | + 'json' => $transactionPayload, |
| 92 | + ]); |
| 93 | + |
| 94 | + $transactionQueryData = json_decode($transactionQueryResponse->getBody()->getContents(), true); |
| 95 | + |
| 96 | + // Check for any errors in the transaction query |
| 97 | + if (isset($transactionQueryData['errors']) && count($transactionQueryData['errors']) > 0) { |
| 98 | + echo "Transaction Error: " . $transactionQueryData['errors'][0]['message'] . "\n"; |
| 99 | + } else { |
| 100 | + echo "Transaction Query Results:\n"; |
| 101 | + print_r($transactionQueryData['data']); // Print transaction results |
| 102 | + } |
| 103 | + |
| 104 | +} catch (RequestException $e) { |
| 105 | + echo "Request Error: " . $e->getMessage() . "\n"; |
| 106 | +} catch (Exception $e) { |
| 107 | + echo "Error: " . $e->getMessage() . "\n"; |
| 108 | +} |
0 commit comments