<?php $context = stream_context_create(array( "http" => [ "timeout" => 3 ], ));
$data = file_get_contents('https://apiv2.bitz.com/Market/kline?symbol=ht_usdt&resolution=1day&size=300', false, $context);
echo "字节", strlen($data),PHP_EOL; if (!$data) { echo '失败',PHP_EOL; exit; }
$dataArr = json_decode($data, true)['data']['bars'] ?? [];
$newDataArr = []; foreach ($dataArr as $ke => $v) { if (empty($v['time'])) continue;
$newDataArr[$v['time']] = [ '开盘' => $v['open'], '收盘' => $v['close'], '最高' => $v['high'], '最低' => $v['low'], '时间' => date('Ymd', ceil($v['time']/1000)), ]; }
create_excel($newDataArr, './ht_usdt');
function create_excel($data = array(), $file_name = '', $file_type = 'csv') { $file_name .= date('Ymd'); $split = ","; if ($file_type == 'xls') { $split = "\t"; } $output = ''; $first = reset($data); foreach ($first as $key => $value) { $output .= $key . $split; } $output .= "\r\n"; foreach ($data as $value) { foreach ($value as $v) { $output .= $v . $split; } $output .= "\r\n"; } $fileName = $file_name . "." . $file_type; $fp = fopen($fileName, "w+"); fwrite($fp, $output); fclose($fp); }
|