drtsd7864 2014-02-19 08:28
浏览 149
已采纳

如何通过PHP curl将此cypher查询(其JSON)执行到Neo4j的REST API?

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条回答 默认 最新

  • drfqfuhej48511519 2014-02-19 09:45
    关注

    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 -d@query.json -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条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置