1
votes

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);
3

3 Answers

2
votes

Disclaimer: I've never used PHP, so I cannot say anything regarding PHP cURL.

On unix shell level, I use curl for sending cypher statements like this:

curl [email protected] -H accept: applicaton/json -H X-Stream:true -H content-type:application/json http://localhost:7474/db/data/cypher

I assume in your snippet the URL is not correct, you have to amend cypher at the end of the url. To debug what is being sent over the wire I recommend using a proxy in between, e.g. charles.

2
votes

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 with a more advanced query:

<?php
 $data=array(
 "query" => "MATCH (actor:Actor { name: {name} })
 SET actor.DoB = 1944
 RETURN actor.name, actor.DoB;",
  "params" => array(
"name" => "Tom Hanks","title" => "Forrest Gump"
  )
 );
$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);
0
votes

Why don't you want to use one of the Neo4j PHP libraries?

See http://neo4j.org/develop/php