Reputation: 243
I am having a problem running this command inside of php's exec command:
UPDATED WORKING CODE:
$results = exec('curl --dump-header - -H "Content-Type: application/json" -X PUT --data @data.json https://website.url --insecure', $output);
if ($results) {
echo "yay!";
var_dump($output);
echo $results;
} else {
var_dump($output);
echo "screw you";
}
originally the script together works in linux but inside php exec the inside single quotes conflicted with php's exec quotes. previous script:
curl --dump-header - -H "Content-Type: application/json" -X PUT --data '{"data": "foo", "data2": "bar"}' https://website.url
I'm wondering what might solve this quotes problem, I thought the escapeshellarg()
might do it but to no avail.
Update:
Error from Error page
PHP Warning: escapeshellarg() expects exactly 1 parameter, 0 given
Upvotes: 0
Views: 3510
Reputation: 157927
This is a typo. Use the []
to access the $_POST
array instead of ()
. Otherwise name and pass would being empty what will break the command line. Further you'll have to escape incoming posts before using it in a shell command. Otherwise the code is vulnerable for shell cmd injections (what is fatal):
$postname = escapeshellarg($_POST['name']);
$postpass = esacpeshellarg($_POST['pass']);
Also you are missing the spaces before and after the json data. Change it to:
$results = exec('curl --dump-header - -H "Content-Type: application/json" -X PUT --data '.escapeshellarg($jsondata). ' https://website.url');
After that changes the example works for me. But you should note about the php curl extension. I would use it instead of calling curl via exec()
Upvotes: 1
Reputation: 751
Try this:
$jsondata = '{"data":"'.$_POST['name'].'", "data2":"'.$_POST['pass'].'"}';
$command = "curl --dump-header - -H \"Content-Type: application/json\" -X PUT --data '$jsondata' https://website.url";
$results = exec($command);
Upvotes: 0