【问题标题】:How to declare CURL body for CoinBase API call in php如何在 php 中为 CoinBase API 调用声明 CURL 主体
【发布时间】: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 上发现的类似问题,但没有一个能解决我的问题。请帮忙!

  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

更新:

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

返回同样的错误

【问题讨论】:

    标签: php curl coinbase-api coinbase-php


    【解决方案1】:

    有些网站可以将命令行 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);
    

    【讨论】:

    • @SMJ 答案已更新。基本上对于每个标题,添加一个新行。
    • 我更新了有问题的代码。你能复习一下吗?如果 $body="" 我确实得到了一个地址。但后来我无法将标签传递给我正在创建的地址。
    • $requestPath 是错误的。它应该是实际的account_id,如/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses
    • 实际上我在原始代码中传递了我的帐户 ID。只是为了 SO 隐藏它
    • 试试这样:$apiurl = "https://api.coinbase.com/v2/";$requestPath = "accounts/actualAccountID/addresses";
    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多