简体   繁体   中英

Running Shell Command Inside PHP

i'm trying to run a shell command inside php code.

My shell command is like this:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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