简体   繁体   English

运行 Shell 内部命令 PHP

[英]Running Shell Command Inside PHP

i'm trying to run a shell command inside php code.我正在尝试在 php 代码中运行 shell 命令。

My shell command is like this:我的 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"
  }
}'

Im trying to run it inside PHP shell_exec() command.我试图在PHP shell_exec()命令中运行它。

I have a lot of problems with quotes.我对报价有很多问题。 What i tried is:我试过的是:

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

?>

When a variable contains the same quotes ( " or ' ) as you're using to open and close it, you need to escape the ones inside using \ . I've fixed your $CMD variable here:当一个变量包含与您用来打开和关闭它相同的引号( "' )时,您需要使用\转义其中的引号。我已在此处修复了您的 $CMD 变量:

$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"\'
}
}\'
';

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

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