英文:
How do I correctly pass query parameters through shell_exec in php
问题
我在一个php脚本中有这个语句:
shell_exec("curl https://$domain/$url.php?importid=$importid&org=$org&t=".urlencode($t)." >> /var/log/bgrd_call.log 2>>&1 ");
nginx/access.log文件显示它正确地用运行时值替换了所有变量。
然而,它只传递了第一个查询参数并截断了行的其余部分。我知道这是因为每当我重新排列查询参数时,第一个参数都会出现在access.log条目中。
首先将参数分配给变量并输出结果会导致这种情况(截断了url部分):
accept_orders.php?importid=268&org=4&t=2023-06-12+14%3A26%3A43 >> /var/log/bgrd_call.log 2>>&1
相应的access.log条目:
[12/Jun/2023:13:30:04 +0000] "GET /accept_orders.php?importid=268 HTTP/1.1" 200 3943 "-" "curl/7.81.0"
正确的做法是什么?
英文:
I have this statement in a php script:
shell_exec("curl https://$domain/$url.php?importid=$importid&org=$org&t=".urlencode($t)." >> /var/log/bgrd_call.log 2>&1 ");
The nginx/access.log file shows it correctly substitutes all variables with runtime values.
However, it only passes the first query parameter and truncates the rest of the line. I know this because whenever I re-arrange the query parameters, the first one shows up in the access.log entry.
First assigning the parameter to a variable and echoing out results in this (cutting off the url portion):
accept_orders.php?importid=268&org=4&t=2023-06-12+14%3A26%3A43 >> /var/log/bgrd_call.log 2>&1
Corresponding access.log entry:
[12/Jun/2023:13:30:04 +0000] "GET /accept_orders.php?importid=268 HTTP/1.1" 200 3943 "-" "curl/7.81.0"
What is the correct way to do this?
答案1
得分: 1
你遇到的问题很可能是由于 &
字符引起的,这在Shell命令行中有特殊含义。
我建议将你的完整URL用 escapeshellarg()
包装起来,像这样:
$url = "https://$domain/$url.php?importid=$importid&org=$org&t=" . urlencode($t);
shell_exec("curl " . escapeshellarg($url) . " >> /var/log/bgrd_call.log 2>&1");
英文:
The problem you are encountering is likely because of the &
character, which has a special meaning on the shell command line.
I suggest wrapping your full URL in escapeshellarg()
like so:
$url = "https://$domain/$url.php?importid=$importid&org=$org&t=".urlencode($t);
shell_exec("curl " . escapeshellarg($url) . " >> /var/log/bgrd_call.log 2>&1");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论