douyan1927 2013-04-04 18:32 采纳率: 0%
浏览 331

在php中运行exec命令不起作用

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

  • 写回答

2条回答 默认 最新

  • dqppv86022 2013-04-04 18:38
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置