OkApi.php 4.5 KB
<?php

namespace jiaoyin;

/*
    所有接口
*/

class OkApi
{
    protected string $host   = 'https://www.okx.com';
    protected string $apikey = '';
    protected string $secret = '';
    protected string $apipwd = '';
    protected string $nonce  = '';
    protected string $signature = '';
    protected array $headers = [];
    protected string $method = '';
    protected array $param = [];
    protected string $path   = '';
    protected array  $options = [];

    public function __construct($apikey = '', $secret = '', $apipwd = '', $host = '')
    {
        $this->apikey = $apikey;
        $this->secret = $secret;
        $this->apipwd = $apipwd;
        if (!empty($host)) {
            $this->host = $host;
        }
    }

    // 核心认证过程
    protected function auth()
    {
        $this->generateNonce();
        $this->generateSignature();
        $this->createHeaders();
    }
    // 生成时间戳
    protected function generateNonce()
    {
        $this->nonce = gmdate('Y-m-d\TH:i:s\.000\Z');
    }

    // 生成签名
    protected function generateSignature()
    {
        $body = '';
        $path = $this->method . $this->path;
        if (!empty($this->param)) {
            if ($this->method == 'GET') {
                $path .= '?' . http_build_query($this->param);
            } else {
                $body = json_encode($this->param);
            }
        }

        $this->signature = base64_encode(hash_hmac('sha256', $this->nonce . $path . $body, $this->secret, true));
    }

    // 构建请求URL
    private function createUrl($path): string
    {
        return $this->host . $path;
    }

    // 构建请求头
    private function createHeaders(): array
    {
        $headers = [
            'Content-Type' => 'application/json',
        ];
        if (!empty($this->apikey) && !empty($this->secret)) {
            $headers = array_merge($headers, [
                'OK-ACCESS-KEY' => $this->apikey,
                'OK-ACCESS-TIMESTAMP' => $this->nonce,
                'OK-ACCESS-PASSPHRASE' => $this->apipwd,
                'OK-ACCESS-SIGN' => $this->signature,
            ]);
        }

        $headers_array = [];
        foreach ($headers as $key => $value) {
            $headers_array[] = $key . ':' . $value;
        }
        $this->headers = $headers_array;
        return $headers;
    }

    /*
     * 公共请求接口
     * $type 1: 需鉴权 2: 不需鉴权
     * */
    public function request($method, $path, $param, $type = 1)
    {
        $this->method = strtoupper($method);
        $this->path = $path;
        $this->param = $param;
        if ($type == 2) {
            $this->createHeaders();
        } else {
            if (empty($this->apikey) || empty($this->secret) || empty($this->apipwd)) {
                return ['code' => -1, 'msg' => 'apikey, secret or apipwd is empty'];
            }
            $this->auth();
        }
        if (!in_array($this->method, ['GET', 'POST', 'DELETE'])) {
            return ['code' => -1, 'msg' => 'Invalid HTTP method'];
        }

        $url = $this->createUrl($path);
        switch ($this->method) {
            case 'GET':
                $data = Curl::httpGet($url, $this->param, $this->headers);
                break;
            case 'POST':
                $data = Curl::httpPost($url, $this->param, $this->headers);
                break;
            case 'DELETE':
                $data = Curl::httpDelete($url, $this->param, $this->headers);
                break;
        }

        return json_decode($data, true);
    }
    /**
     * 合约账户余额查询
     */
    public function getBalance($param = [])
    {
        $path = '/api/v5/account/balance';
        return $this->request('GET', $path, $param);
    }
    /**
     * 资金账户余额查询
     */
    public function getAssetBalance($param = [])
    {
        $path = '/api/v5/asset/balances';
        return $this->request('GET', $path, $param);
    }

    /**
     * 获取持仓信息
     */
    public function getPositions($param)
    {
        $path = '/api/v5/account/positions';
        return $this->request('GET', $path, $param);
    }
    /*
     * 资金划转
     * */
    public function transfer($param)
    {
        $path = '/api/v5/asset/transfer';
        return $this->request('POST', $path, $param);
    }
    /*
     * 公共请求接口
     * */
    public function getTransferHistory($path, $param)
    {
        $path = '/api/v5/asset/bills';
        return $this->request('GET', $path, $param);
    }
}