<?php
/**
 * 数字货币类接口
 * 2019.7.28
 * QQ:748507607 
 */
namespace app\common\model;

use Config;
class DigitalCurrency
{
	private $url = 'https://otc-api.huobi.co/';
	private $api_method = "v1/data/market/detail"; 
	//查询usdt购买汇率
	public function getUsdtExchange()
	{
        $this->req_method = 'GET';
        $res = $this->curl($this->url . $this->api_method);
        $res = json_decode($res, true);
        $data = $res['data']['detail']; 
        if($data)
        {
            foreach($data as $v)
            {
                if($v['coinName'] == 'USDT')
                {
                    $data_exchange =  [
                        'buy'=>$v['buy'],
                        'USDTCNY'=>$v['buy'],
                        'CNYUSDT'=>1/$v['buy']
                    ];
                    return ['code'=>200,'msg'=>'success','data'=>$data_exchange];
                }

            }
        }else{
        	return ['code'=>-1003,'msg'=>'汇率查询失败','data'=>null];            
        }

	}



	private function curl($url,$postdata=[]) {
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL, $url);
		if ($this->req_method == 'POST') {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata));
		}
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_TIMEOUT,60);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
		curl_setopt ($ch, CURLOPT_HTTPHEADER, [
			"Content-Type: application/json",
			]);
		$output = curl_exec($ch);
		$info = curl_getinfo($ch);
		curl_close($ch);
		return $output;
	}



}

 

Logo

加入社区!打开量化的大门,首批课程上线啦!

更多推荐