I am trying to execute the sort of example Neo4j code as found on here:
http://neo4j.com/docs/developer-manual/current/cypher/clauses/create/#create-create-a-full-path however I seem to be missing something obvious. What I need created is (A) has (B).
$n4 = 'curl -H "Accept: application/json; charset=UTF-8" -s -u user:pass -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d \'%s\'';
function basicQuery($query){
global $n4;
$str = '{"query" : "'.$query.'","params" : {}}';
return sprintf($n4,$str);
}
$A = array('Label'=>'Attributes');
$B = array('Label'=>'Attributes');
$query = 'CREATE p =(A '.json_encode($A).')-[:HAS]->(B '.json_encode($B).') RETURN p';
echo shell_exec(basicQuery($query));
But I don't get any output and when I run:
shell_exec(basicQuery('MATCH (A) RETURN DISTINCT count(A) AS tally'));
I get a tally of 0. I'm very new to neo4j so please can someone tell me what I'm doing wrong?
Ok the issue here appeared to be CURL. By using placeholders within my query such as A {Name:{Placeholder}} this seemed to generate output. The solution to solving problems like this appears to be copying the output into the command line and try it out there as well as in PHP.
1) You can't use quote around property key name
2) You need correctly encode a query
$n4 = 'curl -H "Accept: application/json; charset=UTF-8" -s -u user:pass -H "Content-Type: application/json" -X POST http://localhost:7474/db/data/cypher -d \'%s\'';
function basicQuery($query){
global $n4;
$json = array(
"query" => $query,
"params" => new stdClass
);
$str = json_encode($json);
return sprintf($n4, $str);
}
function objToMap($obj) {
$tmp = [];
foreach($obj as $key=>$value) {
$tmp[] = '`' . $key . '`: ' . json_encode($value);
}
return '{' . join(',', $tmp) . '}';
}
$A = array('Label'=>'Attributes');
$B = array('Label'=>'Attributes');
$query = 'CREATE p =(A:TEST ' .objToMap($A).')-[:HAS]->(B:TEST '.objToMap($B).') RETURN p';
echo shell_exec(basicQuery($query));
Related
I have the following curl request to GraphQL. It works great, but in production shell_exex is not allowed. How do I re-write this curl post in valid PHP?
$curl_string = 'curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer "' . AIRTABLE_API_KEY;
$curl_second_string = ' -d \'{"query": "{fullCapaReview (id: \"' . $id . '\") {proposedRuleName submissionDate agencyContactName statusLawDept}}"}\' https://api.baseql.com/airtable/graphql/appXXXzzzzzzzzzz';
$curl_complete_string = "$curl_string $curl_second_string";
$result = shell_exec($curl_complete_string);
edit: I'm sorry, I put the wrong query. The query I had in mind was:
' -d \'{"query": "{dMsAgencies (agencyAcronym: \"' . $_agency . '\") {agencyAcronym fullCapaReview { id }}}"}\'
I make two similar calls. I will leave the original there because someone answered based on that.
This is what I have so far:
$curl = curl_init($url);
$query = 'query dMsAgencies($agencyAcronym: String) {agencyAcronym fullCapaReview { id }} ';
$variables = ["agencyAcronym" => $id ];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['query' => $query, 'variables' => $variables]));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Authorization: Bearer ' . AIRTABLE_API_KEY]);
$response = curl_exec($curl);
curl_close($curl);
console_log("Response : " . $response);
This is the error message I am getting. I just want to see if I am in the ballpark with my syntax.
Response : {"errors":[{"message":"Cannot query field \"agencyAcronym\" on type \"Query\".","locations":[{"line":1,"column":44}],"stack":["GraphQLError: Cannot query field \"agencyAcronym\" on type \"Query\"."," at Object.Field (/var/app/current/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:46:31)"," at Object.enter (/var/app/current/node_modules/graphql/language/visitor.js:323:29)"," at Object.enter (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:370:25)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]},{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}],"stack":["GraphQLError: Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\"."," at Object.leave (/var/app/current/node_modules/graphql/validation/rules/NoUnusedVariablesRule.js:38:33)"," at Object.leave (/var/app/current/node_modules/graphql/language/visitor.js:344:29)"," at Object.leave (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:390:21)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]}]}
message":"Cannot query field \"agencyAcronym\" on type \"Query\"
{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}]
The queries are not the same, but assuming that you are aware, your PHP example also has a syntax issue.
query dMsAgencies($agencyAcronym: String) {
agencyAcronym
fullCapaReview {
id
}
}
If you compare this with the example in the docs (when using variables) you can see that you are currently not using the $agencyAcronym variable anywhere (and there probably isn't a query named agencyAcronym in your schema). Here is one example (using the query from your first snippet):
query dMsAgencies($agencyAcronym: String) {
fullCapaReview (id: $agencyAcronym) {
proposedRuleName
submissionDate
agencyContactName
statusLawDept
}
}
I have the issuses with VK access_token. I added to &access_token= a Secure key of the project, but VKontakte still says that User authorization failed: invalid access_token (4)..
How I can fix it?
$url = 'personasvk';
function get_vk($username) {
$get = get_data('https://api.vk.com/method/users.get?user_id='.$username.'&v=5.85&access_token=SECURE_KEY_OF_PROJECT');
$result = json_decode($get, true);
if (empty($result['response'][0]['gid']) || empty($result['response'][0]['screen_name'])) {
return false;
} else {
return $result;
}
}
$vk_data = get_vk($url);
$title = $vk_data['response'][0]['screen_name'];
$link_id = $vk_data['response'][0]['gid'];
$image = $vk_data['response'][0]['photo'];
echo $title;
Try adding it in the body of your POST request.
I am not familiar with PHP, but this curl gives me all the necessary information.
curl -X POST \
-d 'v=5.85' \
-d 'access_token=SECURE_KEY_OF_PROJECT' \
-d 'user_id=700' \
https://api.vk.com/method/users.get
I have to execute this curl command in php:
curl --digest -u YourApiKey:YourApiSecret "http://api.moodstocks.com/v2/ref/YourID" --form image_file=#"image.jpg" -X PUT
So far I have this:
function ms_addimage($file, $hash_id){
$postdata = array("image_file" => "#/".realpath($file));
$opts[CURLOPT_URL] = $this->API_BASE_URL . "ref/".$hash_id;
$opts[CURLOPT_VERBOSE] =1;
$opts[CURLOPT_POST] =true;
$opts[CURLOPT_POSTFIELDS] =$postdata;
$ch = curl_init();
curl_setopt_array($ch, $opts);
$raw_resp = curl_exec($ch);
echo "Response " . $raw_resp . "\n";
curl_close($ch);
}
The file path is correct but I am missing something.
How do I pass the --form parameter and the PUT argument?
So I was missing the following:
$opts[CURLOPT_CUSTOMREQUEST] = "PUT";
Now it is working.
I'm using this curl command to send json data to a php webservice, but i'm not getting anything in the $_POST variable.
Here is the curl command
curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://192.168.2.127:8888/json.php -d '{"age":"234","password":"password"}'
and here is the php code.
<?php
header('Content-type: application/json');
var_dump($_POST);
$return_arr = array();
$age = $_POST['age'];
$ageInt = intval($_POST['age']);
$return_arr['age1'] = $age;
var_dump($_POST);
$return_arr['age2'] = $ageInt;
echo json_encode($return_arr);
?>
Thanks in advance
Once try this
curl -X POST -H "Content-Type: application/json" -d '{"age":"21343","password":"password"}' http://192.168.2.127:8888/json.php
I run unsuccessfully
$cookie = "masi#gmail.com,777";
$cookie_tripped = trim(",", $_COOKIE['login']);
echo "Cookie: "$cookie_tripped[0];
It gives me
Cookie:
How can you do the following command in PHP?
awk -F, '{ print $1 }'
$cookie = "masi#gmail.com,777";
$cookie_tripped = explode(",", $cookie);
echo "Cookie: " . $cookie_tripped[0];