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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退