php请求okx接口获取比特币价格数据、k线数据

php请求okx接口获取比特币价格数据

环境

我本地用的是thinkphp框架和guzzle

安装guzzle

composer require guzzlehttp/guzzle 

配置请求头、签名

我们需要准备api_key,secret_key,passphrase

api_key,secret_key,passphrase需要我们自己注册账号去申请

官方文档

这是官方的要求
发起请求
所有REST私有请求头都必须包含以下内容:

OK-ACCESS-KEY字符串类型的APIKey。

OK-ACCESS-SIGN使用HMAC SHA256哈希函数获得哈希值,再使用Base-64编码(请参阅签名)。

OK-ACCESS-TIMESTAMP发起请求的时间(UTC),如:2020-12-08T09:08:57.715Z

OK-ACCESS-PASSPHRASE您在创建API密钥时指定的Passphrase。

所有请求都应该含有application/json类型内容,并且是有效的JSON。

签名
生成签名

OK-ACCESS-SIGN的请求头是对timestamp + method + requestPath + body字符串(+表示字符串连接),以及SecretKey,使用HMAC SHA256方法加密,通过Base-64编码输出而得到的。

如:sign=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + ‘GET’ + ‘/api/v5/account/balance?ccy=BTC’, SecretKey))

其中,timestamp的值与OK-ACCESS-TIMESTAMP请求头相同,为ISO格式,如2020-12-08T09:08:57.715Z。

method是请求方法,字母全部大写:GET/POST。

requestPath是请求接口路径。如:/api/v5/account/balance

body是指请求主体的字符串,如果请求没有主体(通常为GET请求)则body可省略。如:{“instId”:“BTC-USDT”,“lever”:“5”,“mgnMode”:“isolated”}

设置签名

 		$api_key = "xxxx";
        $secret_key = "xxxx";
        $passphrase = "xxxx";

        // 设置时区为UTC
        date_default_timezone_set('UTC');

        // 获取当前时间的 DateTime 对象
        $dateTime = new DateTime();

        // 格式化时间戳为指定的格式(ISO 8601)
        $timestamp = $dateTime->format('Y-m-d\TH:i:s.u\Z');

        $url = "";

        $body = "";

        $string = $timestamp . "GET" . $url . $body;

        $signature = base64_encode(hash_hmac('sha256', $string, $secret_key));

        $headers = [
            "OK-ACCESS-KEY" => $api_key,

            "OK-ACCESS-SIGN" => $signature,

            "OK-ACCESS-TIMESTAMP" => $timestamp,

            "OK-ACCESS-PASSPHRASE" => $passphrase
        ];
        
配置代理

请求国外接口需要配置一个代理,我这边本地配置了VPN,所以用的是本地的代理

  		$this->client = new Client([
            "proxy" => "http://127.0.0.1:23457",
            "headers" => $headers
        ]);

全部代码

Res是一个返回类
res.php

<?php
namespace app\util;

class Res{
   
    function success($msg,$data){
   
        return json([
            "code"=>200,
            "msg"=>$msg,
            "data"=>$data
        ]);
    }

    function error($msg){
   
        return json([
            "code"=>400,
            "msg"=>$msg,
            "data"=>null
        ]);
    }
}

okx.php控制器类

<?php

namespace app\controller;

use app\BaseController;
use DateTime;
use think\Request;
use GuzzleHttp\Client;
use app\util\Res;

class Okx extends BaseController
{
   
    private $client;

    private $result;

    public function __construct(\think\App $app)
    {
   
        $api_key = "f5890ab2-a9c8-45bf-a91d-6010be64efbe";
        $secret_key = "ADC1875DA3B14F1BF650EF29BF652E43";
        $passphrase = "XQBxqb123@";

        // 设置时区为UTC
        date_default_timezone_set('UTC');

        // 获取当前时间的 DateTime 对象
        $dateTime = new DateTime();

        // 格式化时间戳为指定的格式(ISO 8601)
        $timestamp = $dateTime->format('Y-m-d\TH:i:s.u\Z');

        $url = "";

        $body = "";

        $string = $timestamp . "GET" . $url . $body;

        $signature = base64_encode(hash_hmac('sha256', $string, $secret_key));

        $headers = [
            "OK-ACCESS-KEY" => $api_key,

            "OK-ACCESS-SIGN" => $signature,

            "OK-ACCESS-TIMESTAMP" => $timestamp,

            "OK-ACCESS-PASSPHRASE" => $passphrase
        ];

        $this->result = new Res();
        $this->client = new Client([
            "proxy" => "http://127.0.0.1:23457",
            "verify" => false,
            "headers" => $headers
        ]);
    }

    public function getPrice($type)
    {
   
        $style = strtoupper($type);
        
        $url = "https://www.okx.com/api/v5/public/mark-price?instType=SWAP&instId={
     $style}-USDT-SWAP";

        $res = $this->client->get($url)->getBody()->getContents();

        return $this->result->success("获取数据成功", json_decode($res));
    }
}

配置路由
route/app.php


Route::group("/okx",function(){
   

    Route::get("/price/:type","okx/getPrice");

});

返回结果
在这里插入图片描述

其他的接口我们只需要在官方文档中对应即可

相关推荐

  1. 获取和莱的实时价格

    2023-12-06 22:52:04       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 22:52:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 22:52:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 22:52:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 22:52:04       20 阅读

热门阅读

  1. 算法----确定两个字符串是否接近

    2023-12-06 22:52:04       29 阅读
  2. React 列表页实现

    2023-12-06 22:52:04       40 阅读
  3. vue管理系统模版

    2023-12-06 22:52:04       39 阅读
  4. UVa1583生成元(Digit Generator)

    2023-12-06 22:52:04       34 阅读
  5. Python 模块的使用方法

    2023-12-06 22:52:04       33 阅读
  6. AIGC: 关于ChatGPT中基于Whisper模型实现音频转文本

    2023-12-06 22:52:04       36 阅读
  7. C语言词法陷阱

    2023-12-06 22:52:04       31 阅读