1

I want use php curl to interact with coinbase api. Simple API calls that does not require data to be passed are successful. What I want to do is create address.
CLI curl works. The command line curl command sample is below:

curl https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses \ -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c' \ -d '{"name": "New receive address"}' }

My php code excerpt

$apiurl = "https://api.coinbase.com";
$secret = "coinbase api secret";
$method = "POST";
$requestPath = "/v2/accounts/actualAccountID/addresses";
$body = "";
$url = $apiurl.$requestPath;
$data["name"] = "curl smj6 ary";
$body=json_encode($data);

$string = $timestamp.$method.$requestPath.$body;
$sig = hash_hmac('sha256', $string, $secret);
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_VERBOSE, true);

if($method == "POST"){

curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}

$headers = [
    "CB-ACCESS-KEY: xxx",
    "CB-ACCESS-SIGN:$sig",
    "CB-ACCESS-TIMESTAMP: $timestamp",
    "CB-VERSION: 2018-03-21",
    "accept: application/json;charset=utf-8"
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute
$result_json = curl_exec($ch);

It returns

{"errors":[{"id":"authentication_error","message":"invalid signature"}]}

Since it works with listing user. I guess th error occurs the way iam passing post data to curl.

Similar Questions that I found on SO but none solves my issue. Please help!

  1. Invalid Signature Coinbase
  2. CoinBase "invalid signature" PHP Buy API Request
  3. How to declare CURL body for CoinBase API call
  4. Api key authentication for coinbase

UPDATE:

$apiurl = "https://api.coinbase.com/v2/";
$requestPath = "accounts/$accountid/addresses";

returns same error

SMJ
  • 716
  • 1
  • 9
  • 23

1 Answers1

0

There are sites that convert the command line cURL syntax to PHP, like https://incarnate.github.io/curl-to-php/

I just pasted your command and:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\": \"New receive address\"}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c";
$headers[] = "CB-ACCESS-KEY: <your api key>";
$headers[] = "CB-ACCESS-SIGN: <the user generated message signature>";
$headers[] = "CB-ACCESS-TIMESTAMP: <a timestamp for your request>";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
IcedAnt
  • 444
  • 3
  • 12
  • @SMJ Answer updated. Basically for each header, add a new line. – IcedAnt Mar 22 '18 at 09:39
  • I updated my code in question. Can you please review that? If $body="" i do get an address. but then i cannot pass label to the address iam creating. – SMJ Mar 22 '18 at 09:57
  • `$requestPath` is wrong. It should be the actual `account_id` like `/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses`. – IcedAnt Mar 22 '18 at 11:05
  • actually i pass my account id in original code. just hide that for SO – SMJ Mar 22 '18 at 11:06
  • Try it like this: `$apiurl = "https://api.coinbase.com/v2/";` `$requestPath = "accounts/actualAccountID/addresses";` – IcedAnt Mar 22 '18 at 11:21
  • still same error. It works on command line as I can pass curl -data How can I do that in php ? – SMJ Mar 22 '18 at 11:29
  • You're already doing it in your code `curl_setopt($ch, CURLOPT_POSTFIELDS, $body);` – IcedAnt Mar 22 '18 at 11:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167335/discussion-between-smj-and-icedant). – SMJ Mar 22 '18 at 11:34