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

如何通过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 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络