如何在php中通过shell_exec正确传递查询参数

huangapple go评论56阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月12日 21:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定