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

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控制器类

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 = "...";

$secret_key = "...";

$passphrase = "...";

// 设置时区为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");

});

返回结果

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

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: