I am using PHP curl to access Neo4j over the REST API and I have encountered one frustration how do I post the cypher query using curl
? Note its already in JSON (the cypher query) and to decode it I'd have to assign it to a PHP variable which I can't because PHP doesn't allow that. I've gone as far as trying to store the query in a MySQL cell and then encoding it but I don't get any response.
PS//I'm new to Neo4j.
The cypher query:
POST http://localhost:7474/db/data/cypher
Accept: application/json; charset=UTF-8
Content-Type: application/json
{
"query" : "MATCH (x {name: {startName}})-[r]-(friend) WHERE friend.name = {name} RETURN TYPE(r)",
"params" : {
"startName" : "I",
"name" : "you"
}
}
Here's my code:
<?php
$con=mysqli_connect('localhost','root','','test');
$sql="select * from json where id=1";
$result=mysqli_query($con,$sql);
$result=mysqli_fetch_assoc($result);
json_decode($result[json]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost:7474/db/data/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS,$result[json]);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Accept: application/json; charset=UTF-8'));
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
curl_setopt($curl,CURLOPT_HTTPHEADER,array('X-Stream: true'));
$result1 = curl_exec($curl);
curl_close($curl);
UPDATE: I finally got it to work by manually decoding the cypher queries to php arrays I finally got it to work after a frustrating couple of hours. It worked after manually changing the cypher queries in neo4j's docs to php arrays then encoding the again. Here's the code: "MATCH (x {name: {startName}})-[r]-(friend) WHERE friend.name = {name} RETURN TYPE(r)", "params" =>array ( "startName" => "I", "name" => "you" ) );
$data=json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost:7474/db/data/cypher/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Accept: application/json; charset=UTF-8','Content-Type: application/json','Content-Length: ' . strlen($data),'X-Stream: true'));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
$result1 = curl_exec($curl);
echo $result1;
curl_close($curl);