美化并解析JSON转储以创建可读的电子邮件内容

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

<?php

$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, 'http://api.postmarkapp.com/email');
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);
curl_close($ch);

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
 SERIAL: C02SQ5888TUA
 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" https://url.com/first.php -d '{ "IP Address": "'"$(dig +short myip.opendns.com @resolver1.opendns.com)"'", "SERIAL": "'"$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')"'", "Date", "'"$(date)"'" }'

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

You can make

$payload = file_get_contents('php://input');
$payload = json_encode(json_decode($payload), JSON_PRETTY_PRINT);