LEN

PHP 抓取 交易对 日线数据生成csv脚本
抓取 ht_usdt 交易对日线数据脚本<?php $context = stream_context_cr...
扫描右侧二维码阅读全文
28
2019/10

PHP 抓取 交易对 日线数据生成csv脚本

抓取 ht_usdt 交易对日线数据脚本

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

 // 创建表格文件 xls,csv
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);
}
最后修改:2019 年 10 月 28 日 10 : 20 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论