币行情实时数据抓取(异域站点网页提取)

币行情实时数据抓取(异域站点网页提取)_第1张图片

 

public function get_coinquotation(){
    /*//获取BTC当前最新行情 - Ticker(宝币网)
    $coin = $_GET['coin'];
    $btc_quotation = get_now_quotation($coin);
    return json($btc_quotation);*/


    $szUrl = "https://www.feixiaohao.com/#USD";
    $UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $szUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);  //0表示不输出Header,1表示输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_ENCODING, '');
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $content_strs = curl_exec($curl);


    $key1 = 'BTC-比特币';
    $key1_end = '/currencies/bitcoin/#markets target=_blank class=volume';
    $key2 = 'ETH-以太坊';
    $key2_end = '/currencies/ethereum/#markets target=_blank class=volume';


    $ex = "/\d+/";
    $txt1=getNeedBetween($content_strs, $key1 , $key1_end );
    $arr1 = [];
    preg_match_all($ex,$txt1,$arr1);
    $btclast = $arr1[0][5];

    $txt2=getNeedBetween($content_strs, $key2 , $key2_end );
    $arr2 = [];
    preg_match_all($ex,$txt2,$arr2);
    $ethlast = $arr2[0][5];

    $data = array([
        'btclast' => $btclast,
        'ethlast' => $ethlast,
    ]);

    return json_encode($data);
}

 

你可能感兴趣的:(【Php】)