You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP版CoinPayments API获取BTC回调地址无返回结果求助

排查PHP版CoinPayments API获取回调地址无返回的问题

看起来你遇到的问题主要来自两个方面:缺少核心的API请求实现,以及代码里的一个小语法错误。咱们一步步来解决:

1. 立即修复的语法错误

在你的GetCallbackAddress方法里,return $this->api_call(...)之后写了echo $this——这行代码永远不会被执行(因为return已经终止了方法运行),而且直接输出对象也没有意义,建议直接删掉这行无效代码。

2. 核心问题:未实现api_call方法

CoinPayments API要求请求必须携带HMAC-SHA512签名,并且通过POST方式发送到官方API端点。你只定义了调用api_call的逻辑,但没有编写这个方法的具体实现,这就是没有返回结果的根本原因。

下面是完整的修复代码,包含api_call方法的实现,以及对原有代码的修正:

class CoinPaymentsAPI {
    private $private_key = '';
    private $public_key = '';
    private $ch = null;
    // CoinPayments官方API端点
    private $api_url = 'https://www.coinpayments.net/api.php';

    public function Setup($private_key, $public_key) {
        $this->private_key = $private_key;
        $this->public_key = $public_key;
        $this->ch = null;
    }

    public function GetCallbackAddress($currency = 'BTC', $ipn_url = '') {
        $req = array(
            'currency' => $currency,
            'ipn_url' => $ipn_url,
        );
        return $this->api_call('get_callback_address', $req);
        // 已删除无效的echo $this代码
    }

    private function api_call($cmd, $req = array()) {
        // 初始化curl客户端(仅初始化一次)
        if ($this->ch === null) {
            $this->ch = curl_init();
            curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($this->ch, CURLOPT_USERAGENT, 'CoinPayments PHP Client');
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2);
        }

        // 组装API请求参数
        $req['cmd'] = $cmd;
        $req['key'] = $this->public_key;
        $req['nonce'] = time() + rand(100000, 999999); // 生成唯一随机数防止重复请求

        // 生成POST格式的数据
        $post_data = http_build_query($req, '', '&');
        // 生成HMAC签名
        $sign = hash_hmac('sha512', $post_data, $this->private_key);

        // 设置curl请求参数
        curl_setopt($this->ch, CURLOPT_URL, $this->api_url);
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
            'HMAC: ' . $sign,
        ));

        // 执行请求并捕获响应
        $response = curl_exec($this->ch);
        if ($response === false) {
            return array('error' => 'Curl请求错误: ' . curl_error($this->ch));
        }

        // 解析JSON响应
        $json = json_decode($response, true);
        if (!$json) {
            return array('error' => '无效的JSON响应');
        }

        return $json;
    }
}

3. 使用示例

确保你传入正确的API公钥和私钥:

// 初始化API客户端
$cp = new CoinPaymentsAPI();
$cp->Setup('你的API私钥', '你的API公钥');

// 获取BTC回调地址(可自定义IPN通知URL)
$result = $cp->GetCallbackAddress('BTC', '你的IPN通知地址');

// 打印结果排查问题
print_r($result);

额外排查点

  • 确认你的服务器已启用curl扩展,且支持SSL请求
  • 检查CoinPayments后台是否已开启API权限,且公钥/私钥填写正确
  • 测试服务器能否正常访问https://www.coinpayments.net(可通过curl命令或在线工具验证连通性)

内容的提问来源于stack exchange,提问作者Mardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:20:25