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
Related
I am trying to get the response of a Http post with curl in Jenkins, I have the following script:
curl -X POST -k -H "Accept: application/json" -H "Content-Type: application/json" --data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" http://mypage/Data/file.php
As you can see I am sending to file.php a json file, and then I am calling some functions and am returning a specific result.
With that script, I am getting the result I want, but I want to evaluate that result, let's say for example the result was "OK", then I want to assign the result to a variable, and then say if $result=="OK" then do this else do that. How can I do that, I have tried something like this:
if $response == "true" then exit 1 fi
But it does not seem to work out, does anyone know how it can be done?
They marked it as similar to this question
PHP cURL, extract an XML response
, but I don't see how, because I am not talking about php code, bash code, and I want to store the curl result in a variable....
Thanks in advance!!!
You can check the status code if you only need to check if the request was successful :
status=$(curl --write-out '%{http_code}' \
-s -o /dev/null \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "200" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
with :
--write-out '%{http_code}' : output the status code
-o /dev/null : doesn't to output the body
-s : doesn't display connection log
As you have specified Accept: application/json, you expect a response in JSON format, so you could use jq JSON parser to parse it :
If the response is :
{ "status": true }
then you can do the following :
status=$(curl -s -H "Accept: application/json" \
-H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php" | jq -r '.status')
if [ "$status" == "true" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
If the response is not in JSON format and the response is OK :
status=$(curl -s -H "Content-Type: application/json" \
--data-binary "#/var/lib/jenkins/workspace/Folder/sessions.json" \
"http://mypage/Data/file.php")
if [ "$status" == "OK" ]; then
echo "request was successful"
else
echo "error status : $status"
fi
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));
So I have a "Skeleton" written for my projects' RESTful API. I've been trying to POST data to the store() function through cURL. And so I used
if(Input::hasfile('file'))
to check if the image/data was posting and it returned false. How would I actually detect the image/data if I were to post it via a cURL request similar to this
curl.exe --user "admin:password" --header "Content-Type: multipart/form-data" --data-binary "#vok.png" --output "out.txt" --dump-header "header.txt" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop/
Ignore the header and response output.
This is my current code for the store() function.
public function store()
{
$file = Input::file('file');
$destinationPath = public_path() . '/uploads';
$filename = $file->getClientOriginalName();
//$extension =$file->getClientOriginalExtension();
$upload_success = Input::file('file')->move($destinationPath, $filename);
if( $upload_success ) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
The issue appears that in your cURL statement
curl.exe --user "admin:password" --header "Content-Type: multipart/form-data" --data-binary "#vok.png" --output "out.txt" --dump-header "header.txt" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop/
You are not passing the file as "file". As you commented earlier on the solution is:
curl.exe -i --user user:pass -F file=#vok.png --output "out.html" http://bitdrops.co/bitdrops/public/index.php/api/v1/drop
I have got the following cURL command that is running expectedly:
curl -X POST
-H "Content-Type: application/json"
-d '{"duplicateEntitiesIds": [21,31,41]}'
{URL}:3003/v1/entities/5/merge
And I am trying to replicate that with Guzzle, which however fails, returning a 400 status code:
$request = $httpClient->post('{URL}:3003/v1/entities/'.$mainEntityId.'/merge',
['json' =>
['duplicateEntitiesIds' => $duplEntitiesIdsToArray]
]
);
$response = $request->send();
I have tried to change my post body , but it keeps failing. Any ideas would be appreciated.
NOTE
The data should be sent in the following format:
{"duplicateEntitiesId": [2,3,4]}
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.