simple php rest call

119 views
Skip to first unread message

Akhil

unread,
Nov 28, 2012, 2:16:02 PM11/28/12
to ne...@googlegroups.com
Hi,
I am new with php and rest and i keep getting the following error ->

"message" : "You have to provide the 'query' parameter.",

when i try to pass a query through rest.Here is my code below.

...............................................

<?php
$URL = "http://localhost:7474/db/data/cypher";
$data = '{ "query" : "START person=node:Person(name = \'Anakin\') RETURN
person",
"params" : {}}';

var_dump(json_decode($data));
var_dump(json_decode($data, true));

function processCurlJsonrequest($URL,$data) { //Initiate cURL request
and send back the result
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type:
application/json'));
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, true);

$results = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
} else {
curl_close($ch);
}
return $results;
}
print_r(processCurlJsonrequest($URL, $data));
?>

Josh Adell

unread,
Nov 28, 2012, 5:24:54 PM11/28/12
to ne...@googlegroups.com, azk...@psu.edu
Your JSON is not valid (according to http://jsonlint.com)
Hand creating JSON as a string is not recommended. PHP's json_encode function will properly escape all strings and turn associative arrays into objects. Try this:

-------------------------------------------------------
$data = json_encode(array(
  "query" => "START person=node:Person(name = 'Anakin') RETURN person", 
  "params" => (object)array(),
)); 
-------------------------------------------------------

I wrote a PHP library for connecting to Neo4j over REST: http://github.com/jadell/neo4jphp
It does Cypher queries, and takes care of all the encoding and curl calls for you. There is an example here: http://github.com/jadell/neo4jphp/blob/master/examples/cypher.php and on the wiki https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries

-- Josh

Josh Adell

unread,
Nov 28, 2012, 7:01:33 PM11/28/12
to ne...@googlegroups.com, azk...@psu.edu
Oops, sorry. The new lines in the post threw me off. The JSON string is valid, but you still shouldn't hand-form JSON. Using json_encode is still the best way to go.

The real problem is you should use "CURLOPT_HTTPHEADER" not "CURLOPT_HTTPHEADERS"

Turn on error reporting in your script to see the warning:
-------------------------------------------------------
error_reporting(-1);
ini_set('display_errors', -1);
-------------------------------------------------------


-- Josh

Akhil

unread,
Nov 29, 2012, 1:03:37 PM11/29/12
to ne...@googlegroups.com, Josh Adell
Thanks Josh, It works now.

Akhil
--
 
 

Reply all
Reply to author
Forward
0 new messages