Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KodaC's avatar
Level 1

Convert curl to http Client

I try to convert a curl request to Laravel http Client.

My problem is that the curl call works, but the one with the http client gives an API error.

Curl:

$nonce = explode(' ', microtime());
$nonce = $nonce[1] . str_pad(substr($nonce[0], 2, 6), 6, '0');
$parameters = [
    'nonce' => $nonce,
];
$postdata = http_build_query($parameters, '', '&');
$path = '/0/private/Balance';
$sign = hash_hmac('sha512', $path . hash('sha256', $nonce . $postdata, true), base64_decode($apiSecret), true);

$headers = [
    'API-Key: ' . $apiKey,
    'API-Sign: ' . base64_encode($sign),
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.kraken.com/0/private/Balance');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
dd($res);

http Client (Replace only the curl part)

$connection = Http::withHeaders($headers);
$response = $connection->post('https://api.kraken.com/0/private/Balance', $parameters);
dd($response->json());

I just don't see my mistake

0 likes
6 replies
cwhite's avatar

Looks like your payload is actually $postdata instead of $parameters?

$response = Http::withHeaders($headers)
    ->post('https://api.kraken.com/0/private/Balance', $postdata);
KodaC's avatar
Level 1

@cwhite Thank you

$parameters is the array and should therefore be correct. But also with $postdate I get the same error.

Debug mode get this error:

curl_setopt_array(): Cannot represent a stream of type Output as a STDIO FILE*

I have now found this:https://laracasts.com/discuss/channels/laravel/post-request-works-with-postman-but-not-guzzle-7

But when I adjust my code with that, I get already the next error

json_encode error: Type is not supported
thinkverse's avatar

By default post is sent as application/json, to send as application/x-www-form-urlencoded use the asForm method. You can read more in the documentation under Sending Form URL Encoded Requests.

$response = Http::withHeaders($headers)
    ->asForm()
    ->post('https://api.kraken.com/0/private/Balance', $postdata);
KodaC's avatar
Level 1

@thinkverse Thank you. But unfortunately I get the same error

When I create a request where it does not require authentication it seems to work. For me this is strange

        $parameters = [
            'pair' => 'XBTUSD',
        ];

        $headers = [
            'Content-Type' => 'application/x-www-form-urlencoded',
        ];

        $connection = Http::withHeaders($headers);
        $response = $connection->post('https://api.kraken.com/0/public/Ticker', $parameters);
        dd($response->json());
thinkverse's avatar
Level 15

I don't know if it might be your signing that's off but this got the correct JSON response for me.

$headers = [
    'API-Key' => config('services.kraken.key'),
    'API-Sign' => base64_encode(
        hash_hmac(
            'sha512',
            '/0/private/Balance' . hash('sha256', $nonce . http_build_query($parameters), true),
            base64_decode(config('services.kraken.secret')),
            true,
        )
    )
];

$response = Http::withHeaders($headers)
    ->asForm()
    ->post('https://api.kraken.com/0/private/Balance', $parameters);

dd($response->json());

Here dd returns the expected JSON response data.

{"error":[],"result":{}}

Also, make sure your API Key has the correct permission to Query funds.

KodaC's avatar
Level 1

@thinkverse Thank you. I don't understand. Mayby my file was not save. Now it works.

1 like

Please or to participate in this conversation.