调用gate.io的接口获取币种及当前价格

api = '1/pairs';
		$url = $this->base.$this->api;
		$json = $this->curl_file_get_contents($url);
        dp($json);
		return $json;
	}
    /**
     * @title 获取某币最新币价
     * @description 获取gateio的某币最新价格
     * @author 开发者
     * @url /api/binance/get_common_symbols
     * @method GET
     *
     * @param $symbol string 例:BTCUSDT
     */
    public function get_ticker(){
        $current_pairs = 'btc_usdt';
        $this->api = '1/ticker/';
        $url = $this->base.$this->api.strtoupper($current_pairs);
        $json = $this->curl_file_get_contents($url);
        dp($json);
        return $json;
    }

    /**
     * 请求
     */
    protected function curl_file_get_contents($url) {
        static $ch = null;
        if (is_null($ch)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT,
                'Mozilla/4.0 (compatible; gate PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
            );
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        // run the query
        $res = curl_exec($ch);
        if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
        $dec = json_decode($res, true);
        if (!$dec) throw new Exception('Invalid data: '.$res);

        return $dec;
    }

}

注:由于接口需在外网测试,建议采用外网服务器

你可能感兴趣的:(调用gate.io的接口获取币种及当前价格)