我正在使用PHP curl通过REST API访问Neo4j,我遇到了一个令人沮丧的问题,如何使用curl
发布密码查询?注意它已经在JSON(密码查询)并解码它我必须将它分配给PHP变量,我不能,因为PHP不允许这样做。我已经尝试将查询存储在MySQL单元格中,然后对其进行编码,但我没有得到任何响应。
PS //我是Neo4j的新手。
密码查询:
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"
}
}
这是我的代码:
<?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);
更新:我终于通过手动将cypher查询解码为php数组来实现它 经过一段令人沮丧的几个小时后,我终于开始工作了。它在neo4j的docs中手动更改cypher查询到php数组然后再次编码后工作。这是代码: “MATCH(x {name:{startName}}) - [r] - (朋友)WHERE friend.name = {name} RETURN TYPE(r)”, “params”=&gt;数组( “startName”=&gt; “一世”, “name”=&gt; “您” ) );
$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 :(得分:2)
免责声明:我从未使用过PHP,所以我对PHP cURL一无所知。
在unix shell级别上,我使用curl发送如下的cypher语句:
curl -d@query.json -H accept: applicaton/json -H X-Stream:true -H content-type:application/json http://localhost:7474/db/data/cypher
我认为在您的代码段中,网址不正确,您必须在网址末尾修改cypher
。为了调试通过网络发送的内容,我建议在两者之间使用代理,例如charles
答案 1 :(得分:2)
我终于通过手动解码cypher查询到php数组来工作了我终于在一段令人沮丧的几个小时之后才开始工作了。它在neo4j的docs中手动更改cypher查询到php数组然后再次编码后工作。这是具有更高级查询的代码:
<?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);
答案 2 :(得分:0)
为什么不想使用其中一个Neo4j PHP库?