在PHP中运行Shell命令

jm81lzqq  于 2022-11-30  发布在  Shell
关注(0)|答案(1)|浏览(836)

我正在尝试运行一个shell命令里面的php代码。
我的shell命令是这样的:

curl -X POST "https://supergoodwebsite.com/v2/SOMENUMBERS/supplement.json" \
     -H "Api-Key:RGNROK-H219R32" \
     -i \
     -H "Content-Type: specification/json" \
     -d \
'{
  "deployment": {
    "revision": "REVISION",
    "changelog": "",
    "description": "Some Description",
    "user": "MasterUser"
  }
}'

我试图在**PHP shell_exec()**命令中运行它。
我对引用有很多问题。我试过的是:

<?php

$APP_ID = 38993011;
$DESCRIPTION = "deneme";
$USER = "superuser";

$CMD = 'curl -X POST "https://superwebsite/v2/"$APP_ID"/supplements.json" \
-H "Api-Key:SAAK-EJRV2PDLKGES0L7NTT" \
-i \
-H "Content-Type: specification/json" \
-d \
'{
"deployment": {
"revision": "REVISION",
"changelog": "",
"description": '"$DESCRIPTION"',
"user": '"$USER"'
}
}'
';

$output = shell_exec($CMD);

print($output);

?>
u5i3ibmn

u5i3ibmn1#

当一个变量包含的引号("')与你用来打开和关闭它的引号相同时,你需要使用\来转义里面的引号。

$CMD = 'curl -X POST "https://superwebsite/v2/"$APP_ID"/supplements.json" \
-H "Api-Key:SAAK-EJRV2PDLKGES0L7NTT" \
-i \
-H "Content-Type: specification/json" \
-d \
\'{
"deployment": {
"revision": "REVISION",
"changelog": "",
"description": \'"$DESCRIPTION"\',
"user": \'"$USER"\'
}
}\'
';

相关问题