PHP代码整理返回的数据处理成想要的内容,内容如下

liuliang.php 代码如下

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

<?php error_reporting(0);
$handle = fopen ("http://www.xxx.com/user_get_xxxx_xxxx?token=3ePdlaQDXxyNnCmA1613651742565", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents.= $data;
} while(true);
fclose ($handle);

echo $contents;


?>

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

打开liuliang.php  返回的数据

{"code": 0, "data": {"user_id": 3205, "residential_total_rate": 23530120, "mobile_total_rate": 20232, "resstatic_total_rate": 4655665, "total_longtime_ip_count": 0, "valid_longtime_ip_count": 0, "current_month_residential_cost_amount": 28, "current_month_mobile_cost_amount": 0, "current_month_resstatic_cost_amount": 9, "current_month_residential_rate": 23530120, "current_month_mobile_rate": 0, "current_month_resstatic_rate": 4655665}, "msg": "success"}

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

echo $contents;    需要把以上返回的数据 整理 以下3项后面数字是表示的字节数,想直接换算成MB显示出来方便看

residential_total_rate": 23530120

mobile_total_rate": 20232

resstatic_total_rate": 4655665

 数字是表示的字节数,想直接换算成GB显示出来方便看,并且把residential_total_rate和mobile_total_rate 和resstatic_total_rate替换成中文其他的数据都不要就向下列这样显示,应该怎么写

住宅已用流量:XXX  MB

移动已用流量:XXX  MB

静态已用流量:XXX  MB

 

<?php
$contents='{"code": 0, "data": {"user_id": 3205, "residential_total_rate": 23530120, "mobile_total_rate": 20232, "resstatic_total_rate": 4655665, "total_longtime_ip_count": 0, "valid_longtime_ip_count": 0, "current_month_residential_cost_amount": 28, "current_month_mobile_cost_amount": 0, "current_month_resstatic_cost_amount": 9, "current_month_residential_rate": 23530120, "current_month_mobile_rate": 0, "current_month_resstatic_rate": 4655665}, "msg": "success"}';
$o=json_decode($contents);

//如果计数单位是比特而不是字节,记得下面的计算还得除以8.
$rst='住宅已用流量:'.round($o->data->residential_total_rate/1024/1024,2).' MB<br>'
.'移动已用流量:'.round($o->data->mobile_total_rate/1024/1024,2).' MB<br>'
.'静态已用流量:'.round($o->data->resstatic_total_rate/1024/1024,2).' MB<br>';
echo $rst;
?>

 

 

$contents='{"code": 0, "data": {"user_id": 3205, "residential_total_rate": 23530120, "mobile_total_rate": 20232, "resstatic_total_rate": 4655665, "total_longtime_ip_count": 0, "valid_longtime_ip_count": 0, "current_month_residential_cost_amount": 28, "current_month_mobile_cost_amount": 0, "current_month_resstatic_cost_amount": 9, "current_month_residential_rate": 23530120, "current_month_mobile_rate": 0, "current_month_resstatic_rate": 4655665}, "msg": "success"}';
$o=json_decode($contents,true);


//1GB = 1024MB 1MB = 1024KB 1KB = 1024Bit
$rst ='住宅已用流量:'.number_format($o['data']['residential_total_rate']/1024/1024/1024,5,".","").' GB<br>';
$rst.='移动已用流量:'.number_format($o['data']['mobile_total_rate']/1024/1024/1024,5,".","").' GB<br>';
$rst.='静态已用流量:'.number_format($o['data']['resstatic_total_rate']/1024/1024/1024,5,".","").' GB<br>';