简体   繁体   中英

running exec command in php does not function

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

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()

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM