Home > Back-end >  Running Shell Command Inside PHP
Running Shell Command Inside PHP

Time:11-29

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

?>

CodePudding user response:

Your Variable $CMD is wrong.

But, you can use php curl: https://www.php.net/manual/en/book.curl.php

try this one:

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

CodePudding user response:

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