0

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

1
  • Compared to your previous command, you seem to be missing spaces with around your json data. Commented Apr 4, 2013 at 18:35

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

18 Comments

we had tried that but to no avail :( we are interfacing with tastypies rest system.
Unfortunately I cannot test without having an account there. Have you tried using the curl extension? You'll find a ton of examples online. But can help if you need
Can you add the exact url you are trying to the question?
I can not as it includes a api key and secret but the url is not the problem, the problem comes when I try to input the json data
woot! got it to work. I'm updating the code above to new working code.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.