繁体   English   中英

PHP shell_exec($cmd) 命令不适用于变量

[英]PHP shell_exec($cmd) command not working with variables



我有一个 php 脚本,它使用 shell_exec 命令运行带有 json 数据的 http 发布请求。
PHP function 接收 json 编码数据和 Z572D4E421E5E6B9BC111D815E8A20 作为变量。
我正在尝试使用上述变量构建要发送到 shell_exec 命令的字符串。

正如您在下面的示例代码中看到的,第一个字符串 ($cmd) 具有 $jsonDataEncoded 作为变量(url 是明文)。 该字符串与 shell_exec 命令配合良好,并且从服务器返回 json 数据。

第二个 ($cmd1) 将 $jsonDataEncoded 和 $url 作为变量。 此字符串不适用于 shell_exec 命令。

我试图比较两个字符串,即使是十六进制比较,它们看起来很相似。
谢谢你的建议。

这是代码:

    function HttpPost ($url, $jsonDataEncoded)
{
//$url=https://cloud.sample.com/web/api2/v1/auth/login
//$jsonDataEncoded= my encoded json string.

//the following string is working
$cmd = "curl -X POST -H \"Content-Type: application/json\" \
 -d '".$jsonDataEncoded. "' \
 https://cloud.sample.com/web/api2/v1/auth/login";

//the following string is NOT working
$cmd1 = "curl -X POST -H \"Content-Type: application/json\" \
 -d '".$jsonDataEncoded. "' \ ". $url;

$result = shell_exec($cmd);//WORKS
//$result = shell_exec($cmd1);//FAILS
$json = json_decode($result, true);
return $json;
}

您应该删除最后一个\字符,它需要在新行上输入。 所以这应该工作:

$cmd1 = "curl -X POST -H \"Content-Type: application/json\" \
 -d '" . $jsonDataEncoded . "' " . $url;

顺便说一句,使用 shell_exec 不是在 PHP 中发送 POST 请求的正确方法。 您可以为此使用库:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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