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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器