【发布时间】:2018-03-22 08:22:55
【问题描述】:
我想使用 php curl 与 coinbase api 交互。不需要传递数据的简单 API 调用是成功的。我想做的是create address.
CLI curl 有效。命令行 curl 命令示例如下:
卷曲https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses\ -X 发布\ -H '内容类型:应用程序/json' \ -H '授权:承载abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c' \ -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 上发现的类似问题,但没有一个能解决我的问题。请帮忙!
- Invalid Signature Coinbase
- CoinBase "invalid signature" PHP Buy API Request
- How to declare CURL body for CoinBase API call
- Api key authentication for coinbase
更新:
$apiurl = "https://api.coinbase.com/v2/";
$requestPath = "accounts/$accountid/addresses";
返回同样的错误
【问题讨论】:
标签: php curl coinbase-api coinbase-php