|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/../vendor/autoload.php'; // Ensure you include the autoloader for your dependencies |
| 4 | + |
| 5 | +use Neo4j\QueryAPI\Neo4jQueryAPI; |
| 6 | +use Neo4j\QueryAPI\Objects\Authentication; |
| 7 | + |
| 8 | +try { |
| 9 | + // Configuration |
| 10 | + $neo4jHost = 'https://6f72daa1.databases.neo4j.io/'; // Replace with your Neo4j host |
| 11 | + $username = 'neo4j'; // Replace with your Neo4j username |
| 12 | + $password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0'; // Replace with your Neo4j password |
| 13 | + $database = 'neo4j'; // Default database (replace if needed) |
| 14 | + |
| 15 | + // Authentication setup |
| 16 | + $auth = Authentication::basic($username, $password); |
| 17 | + |
| 18 | + // Initialize the Neo4jQueryAPI instance |
| 19 | + $neo4j = Neo4jQueryAPI::login($neo4jHost, $auth); |
| 20 | + |
| 21 | + // Run a sample Cypher query |
| 22 | + $cypherQuery = 'MATCH (n) RETURN n LIMIT 10'; // Replace with your desired query |
| 23 | + $parameters = []; // Optional query parameters |
| 24 | + |
| 25 | + $resultSet = $neo4j->run($cypherQuery, $parameters, $database); |
| 26 | + |
| 27 | + // Output the results |
| 28 | + echo "Query executed successfully!\n"; |
| 29 | + foreach ($resultSet->getRows() as $row) { |
| 30 | + echo json_encode($row->toArray(), JSON_PRETTY_PRINT) . "\n"; |
| 31 | + } |
| 32 | + |
| 33 | + // Output result counters |
| 34 | + $counters = $resultSet->getCounters(); |
| 35 | + echo "Nodes created: " . $counters->getNodesCreated() . "\n"; |
| 36 | + echo "Nodes deleted: " . $counters->getNodesDeleted() . "\n"; |
| 37 | +} catch (Exception $e) { |
| 38 | + // Handle errors |
| 39 | + echo 'Error: ' . $e->getMessage() . "\n"; |
| 40 | + if ($e instanceof \Neo4j\QueryAPI\Exception\Neo4jException) { |
| 41 | + echo 'Neo4j Error Details: ' . json_encode($e->getErrorDetails(), JSON_PRETTY_PRINT) . "\n"; |
| 42 | + } |
| 43 | +} |
0 commit comments