1

我想使用 php curl 与 coinbase api 交互。不需要传递数据的简单 API 调用是成功的。我想做的是创建地址
CLI curl 有效。命令行 curl 命令示例如下:

curl https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses \ -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08ce2c2c340cd53e08ce2c2c ' \ -d '{"name": "新接收地址"}' }

我的 php 代码摘录

$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);

它返回

{"errors":[{"id":"authentication_error","message":"无效签名"}]}

因为它适用于列表用户。我想错误发生在我将发布数据传递给 curl 的方式。

我在 SO 上发现的类似问题,但没有一个能解决我的问题。请帮忙!

  1. 无效的签名 Coinbase
  2. CoinBase“无效签名”PHP Buy API 请求
  3. 如何为 CoinBase API 调用声明 CURL 主体
  4. coinbase 的 API 密钥认证

更新:

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

返回相同的错误

4

1 回答 1

0

有些网站可以将命令行 cURL 语法转换为 PHP,例如https://incarnate.github.io/curl-to-php/

我刚刚粘贴了您的命令,并且:

// 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);
于 2018-03-22T08:27:07.310 回答