2017-06-23 18:44



I have a little php script on the server which takes a JSON dump of data from a curl command on a remote server.

The php script e-mails me via postmark the result however it just e-mails the raw JSON data at the moment which can be obviously quite unreadable.

I'd like to beautify and then parse the data if possible before sending it.

So the current php script is


$payload = file_get_contents('php://input');
$serverToken = "*****";

 $json = json_encode(array(
    'From' => 'email',
    'To' => 'email',
    'Subject' => 'Server Info',
    'HtmlBody' => $payload,
    'TextBody' => $payload

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Content-Type: application/json',
    'X-Postmark-Server-Token: ' . $serverToken
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$response = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

and I'd get e-mailed this...

{ "IP": "78.122.144", "SERIAL" : "C02SQ5888TUA", "Date", "Fri 23 Jun 2017 18:05:01 BST" }

A beautifier online would turn it into

"IP": "78.122.144",
"SERIAL": "C02SQ5888TUA",
"DATE": "Fri 23 Jun 2017 18:05:01 BST"

But i'd really like the content of the email to look like this...

 IP: 78.122.144
 DATE: Fri 23 Jun 2017 18:05:01 BST

With the {} " and , removed.

Could anyone suggest how to have the script/server do that, and the icing on the cake would be to put the serial number above into the subject of the email too.

An example curl command is:

curl -H "Content-Type: application/json" -d '{ "IP Address": "'"$(dig +short"'", "SERIAL": "'"$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')"'", "Date", "'"$(date)"'" }'
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongtazu3080 dongtazu3080 4年前

    You could do this (replace the first line in your script with this)

     $payload = file_get_contents('php://input');
     $jsonArray = json_decode($payload);
     $payload = "";
     foreach($jsonArray as $key => $value){
          $payload .= $key . ": " . $value . "

    That should make payload a pretty string.

    To add the serial number to the email subject, all you'd need to do is

    'Subject' => "Server Info " . $jsonArray['SERIAL'],

    Or you could just do

    $payload = file_get_contents('php://input');
    $payload = print_r($payload, true);
    点赞 评论 复制链接分享
  • dstt1818 dstt1818 4年前

    You can make

    $payload = file_get_contents('php://input');
    $payload = json_encode(json_decode($payload), JSON_PRETTY_PRINT);
    点赞 评论 复制链接分享