Nileshkumar
Nileshkumar

Reputation: 125

Curl is returning response but Laravel Http client is not returnig respose

-> The below code is based on php Curl.

$curl = curl_init();
// API Endpoint
$url = 'https://api.bybit.com/fht/compliance/tax/v3/private/registertime';

curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
    'X-BAPI-SIGN: ' . $sign,
    'X-BAPI-API-KEY: ' . $apikey,
    'X-BAPI-TIMESTAMP: ' . $timestamp,
    'X-BAPI-RECV-WINDOW: ' . $recvWindow,
    'X-BAPI-SIGN-TYPE: 2', 
    'Content-Type: application/json'
),
));

// Execute cURL request and fetch response
$response = curl_exec($curl);

-> The below code is based on Laravel Http Client method

$response = Http::withHeaders(array(
    'X-BAPI-SIGN' => $sign,
    'X-BAPI-API-KEY' => $apikey,
    'X-BAPI-TIMESTAMP' => $timestamp,
    'X-BAPI-RECV-WINDOW' => $recvWindow,
    'X-BAPI-SIGN-TYPE' => '2',
    'Content-Type' => 'application/json',
))->post('https://api.bybit.com/fht/compliance/tax/v3/private/registertime');
dd($response->json());

Note: Other paramter which is passing in both curl and http client which is same but we are not getting response in http client.

Successfully response from curl is

{"retCode":0,"retMsg":"","result":{"registerTime":"1562630400"},"retExtInfo":{},"time":1732258576995}

Error response from http client is

array:5 [
  "retCode" => 10004
  "retMsg" => "error sign! origin_string[1732259721000OXG2zzIOaiymlOWj0B5000[]]"
  "result" => []
  "retExtInfo" => []
  "time" => 1732259721441
]

--> Can you please help me to get out this error.

public function bybit_taxPrivateRegisterTime(Request $request)
    {
        try {
            $apikey = $request->input('apikey');
            $apisecret = $request->input('apisecret');
            $recvWindow = '5000';
            $timestamp = time() * 1000; // Use time in milliseconds

            // Correctly build the signString (timestamp + apiKey + recvWindow) as in the cURL example
            $signString = $timestamp . $apikey . $recvWindow;

            // Generate signature using HMAC SHA256
            $sign = hash_hmac('sha256', $signString, $apisecret);

            // Headers with proper order
            $headers = [
                'X-BAPI-SIGN' => $sign,
                'X-BAPI-API-KEY' => $apikey,
                'X-BAPI-TIMESTAMP' => $timestamp,
                'X-BAPI-RECV-WINDOW' => $recvWindow,
                'X-BAPI-SIGN-TYPE' => '2',  // This can be '2' or another value depending on the Bybit API version
                'Content-Type' => 'application/json',  // Ensure the request is sent as JSON
            ];

            // Make the request with headers
            $response = Http::withHeaders($headers)
                            ->post('https://api.bybit.com/fht/compliance/tax/v3/private/registertime');

            $curl = curl_init();
            // API Endpoint
            $url = 'https://api.bybit.com/fht/compliance/tax/v3/private/registertime';

            curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_HTTPHEADER => array(
                    'X-BAPI-SIGN: ' . $sign,
                    'X-BAPI-API-KEY: ' . $apikey,
                    'X-BAPI-TIMESTAMP: ' . $timestamp,
                    'X-BAPI-RECV-WINDOW: ' . $recvWindow,
                    'X-BAPI-SIGN-TYPE: 2', 
                    'Content-Type: application/json'
                ),
            ));

            // Execute cURL request and fetch response
            $curl_response = curl_exec($curl);
            dd($response->json(), $curl_response);

            return response()->json($response->json());
        } catch (\Exception $e) {
            return response()->json([
                'error' => 'API request failed',
                'message' => $e->getMessage()
            ], 500);
        }
    }

The below is function and output is given below

array:5 [
  "retCode" => 10004
  "retMsg" => "error sign! origin_string[1732260150000OXG2zzIOaiymlOWj0B5000[]]"
  "result" => []
  "retExtInfo" => []
  "time" => 1732260150743
]

{"retCode":0,"retMsg":"","result":{"registerTime":"1562630400"},"retExtInfo":{},"time":1732260150914}

Upvotes: 0

Views: 43

Answers (0)

Related Questions