猿问

Coinbase Commerce Webhooks API PHP

我是PHP开发的新手


我正在尝试通过实施一些有趣的真实项目来学习。因此,我尝试构建一个比特币应用程序,让客户可以用加密货币付款。


所以我从Coinbase商业API开始


我成功实现了收费页面,并且一切正常,直到到达必须处理WEBHOOKS的地步?


我正在关注本文档


https://github.com/coinbase/coinbase-commerce-php/blob/master/README.md

这就是WEBHOOKs的代码


`<?php

require_once __DIR__ . "/vendor/autoload.php";

use CoinbaseCommerce\Webhook;

/**

 * To run this example please read README.md file

 * Past your Webhook Secret Key from Settings/Webhook section

 * Make sure you don't store your Secret Key in your source code!

 */

$secret = 'SECRET_KEY';

$headerName = 'X-Cc-Webhook-Signature';

$headers = getallheaders();

$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : null;

$payload = trim(file_get_contents('php://input'));

try {

    $event = Webhook::buildEvent($payload, $signraturHeader, $secret);

    http_response_code(200);

    echo sprintf('Successully verified event with id %s and type %s.', $event->id, $event->type);

} catch (\Exception $exception) {

    http_response_code(400);

    echo 'Error occured. ' . $exception->getMessage();

}

`

当我访问we hooks URL时,出现此错误


Error occured. Invalid payload provided. No JSON object could be decoded

请?我希望有人向我解释此错误


慕妹3242003
浏览 190回答 2
2回答

三国纷争

似乎您正在向期望从Web挂钩发出POST(具有有效载荷数据)请求的URL发出GET(无有效载荷数据)请求。要使用POST,PUT,GET请求测试API,可以使用PostMan之类的工具。您可以手动构建JSON负载并测试端点。

慕姐8265434

试试这个$headerName = 'x-cc-webhook-signature';$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : null;代替$headerName = 'X-Cc-Webhook-Signature';$signraturHeader = isset($headers[$headerName]) ? $headers[$headerName] : null;
随时随地看视频慕课网APP
我要回答