-1

I am trying to connect to the Bitpanda website using the php curl to retrieve my crypto assets.

Unfortunately it doesn't work, I'm adding a Powershell script that works, I hope someone can translate it into php...

I get the following error message:

{"errors":[{"status":401,"code":"unauthorized","title":"Credentials\/Access token wrong"}]}

Here is the example from Bitpanda:


curl -X GET "https://api.bitpanda.com/v1/trades" \
    -H "X-API-KEY: string"

If the solution is in javascript, that would be enough for me :)

Thanks in advance

php don´t work:

    $service_url = "https://api.bitpanda.com/v1/trades";
    $curl_session = curl_init($service_url);

    curl_setopt($curl_session, CURLOPT_USERPWD, $apiKey);
    curl_setopt($curl_session, CURLOPT_HTTPHEADER, ['content-type: application/json']);
    curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($curl_session);
    curl_close($curl_session);
    echo $result;

powershell works:

$key = "APIKEY"
$trades = Invoke-WebRequest -Uri "https://api.bitpanda.com/v1/trades" -Headers @{"X-API-KEY" = $key} -UseBasicParsing
$tdata = $trades.Content | ConvertFrom-Json
$tdata.data.attributes | Out-GridView
LLoLoX
  • 3
  • 2
  • In the curl example, they use `-H`. If you look at [the curl docs](https://curl.se/docs/manpage.html#-H), you'll see that means to pass your API key as a header. You're using `-Headers` in your Powershell example, just as they describe, and it works. You'll need to pass it as a header in PHP too - `CURLOPT_USERPWD` is not a header. – Don't Panic Nov 10 '22 at 22:12
  • If you search for "*php curl api key*" you'll find quite a few examples here on SO that show how to pass an API key (and other values) as a header - [here's one](https://stackoverflow.com/questions/26495065/php-using-api-key-in-curl-get-call). If you search for "*php curl X-API-KEY*" you even find some showing how to pass exactly that header, [here's one](https://stackoverflow.com/questions/43049281/how-to-authenticate-an-api-in-php): `curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-API-Key: $apiKey"]);` – Don't Panic Nov 10 '22 at 22:14
  • Does this answer your question? [How to Authenticate an API in PHP?](https://stackoverflow.com/questions/43049281/how-to-authenticate-an-api-in-php) – Don't Panic Nov 10 '22 at 22:15
  • Possible duplicate – Harsh Nov 11 '22 at 05:44

1 Answers1

0

Try this:

$header = array('X-API-KEY' => 'string');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.bitpanda.com/v1/trades');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header );

$response = curl_exec($ch);
Misunderstood
  • 5,534
  • 1
  • 18
  • 25