如何在php中为CoinBase API调用声明CURL主体

时间:2018-03-22 08:22:56

标签: php curl coinbase-api coinbase-php

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

  

卷曲https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses \     -X POST \     -H'内容类型:application / json'\     -H'授权:bearer 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”:“invalid signature”}]}

因为它适用于列表用户。我猜这个错误发生在iam传递post数据到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
  5. 更新:

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

    返回相同的错误

1 个答案:

答案 0 :(得分:0)

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

我刚刚粘贴了你的命令并且:

CommandBar