简体   繁体   中英

Bash JSON CURL request not working along with PHP script

I'am writing some automation script in bash and in final step I want that script send some data to remote server via CURL. Data is in JSON format, data receiver is written in PHP.

I have done numerous tests, but no luck to receive answer from PHP script after CURL request is made.

I've done this:

BASH side (sender):

#!/bin/bash
json='
{
  "website_url": "${site_url}",
  "web_dir": "${www_dir}",
  "php_fpm_version": "7.3",
  "server_id": "1"
}'


echo "${json}" | curl --request POST "https://website.tld/api/api.php" \
-H "Content-Type: application/json" \
-d @-

PHP side (receiver):

<?php

print_r($_POST);

Expected results:

Array(
{
  "website_url": "${site_url}",
  "web_dir": "${www_dir}",
  "php_fpm_version": "7.3",
  "server_id": "1"
}
)

Actual results:

Aray
(
)

You can get the JSON data by processing the raw input. For example changing your PHP-file to this:

<?php

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);

Found a problem: spaces in JSON array at the beginning of each line. Just done this:

json='{
"account": {
"wordpress": {
"username": "${username}",
"password": "${wp_user_pass}"
},
"database": {
"dbname":   "${username}",
"username": "${username}",
"password": "${dbpass}"
},
"ftp": {
"username": "${username}",
"password": "${ftp_user_pass}"
}
},
"website_url": "${site_url}",
"web_dir": "${www_dir}",
"php_fpm_version": "7.3",
"server_id": "1"
}'

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