dongqiao1151 2017-06-23 18:44
浏览 137
已采纳

美化并解析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)"'" }'
  • 写回答

2条回答 默认 最新

  • dongtazu3080 2017-06-23 19:04
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题