MohsenCreative
MohsenCreative

Reputation: 84

Connect to coinex API using PHP laravel

I'm trying to connect to the Coinex API and get balance using PHP. I followed the documentation provided on their web site but keep encountering a signature error message.

I've attempted several changes in my code but haven't been able to resolve the issue.

Could anyone help me understand what might be causing this error and how to properly connect to the API? Any insights or examples would be greatly appreciated.

Coinex API Documentation for auth :https://docs.coinex.com/api/v2/authorization

Coinex API Documentation for get balance: https://docs.coinex.com/api/v2/assets/balance/http/get-spot-balance

Here's a snippet of my code for API class reference:

<?php

namespace App\Classes\Api;


use GuzzleHttp\Client;


class CoinexFuturesApi{


    public string $apiKey = "";
    public string $secretKey = "";




    public function  get_balance(){

        $client = new Client();


        $timestamp = (int)(microtime(true)*1000);

        $prepString = "GET"."/assets/spot/balance".$timestamp;

        $key = $this->secretKey;
        $message = $prepString;

        $signed_str = hash_hmac('sha256', $message, $key);

        $signed_str = strtolower($signed_str);


        $response = $client->get("https://api.coinex.com/v2/assets/spot/balance",[
            'headers' => [
                "X-COINEX-KEY" =>$this->apiKey,
                "X-COINEX-SIGN" => $signed_str,
                "X-COINEX-TIMESTAMP" =>$timestamp
            ],
            ]);

        $response = json_decode($response->getBody()->getContents(),false,512,JSON_THROW_ON_ERROR);

        return $response;

    }

}

And for controller class:

<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Classes\Api\CoinexFuturesApi;

class TestController extends Controller
{
    public function __construct(){

        $this->middleware('auth');
    }


    public function index(){
        $cx = new CoinexFuturesApi();


        $response = $cx->get_balance();


        echo '<pre>';
        print_r($response);
        echo '</pre>';



    }


}

Error I get:

enter image description here

Upvotes: 1

Views: 35

Answers (0)

Related Questions