[英]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.