我想使用 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 上发现的类似问题,但没有一个能解决我的问题。请帮忙!
更新:
$apiurl = "https://api.coinbase.com/v2/";
$requestPath = "accounts/$accountid/addresses";
返回相同的错误