php - 我从用于 Stripe 事件的 webhook url 收到空白响应
问题描述
我从条纹 webhook url 得到空白响应。
我正在使用以下代码:
$json = file_get_contents('php://input');
$action = json_decode($json, true);
但是在$json数组中,我得到了空白响应。
解决方案
我假设你的意思是$action
数组是空的。我的猜测是——而且我从来没有看过那里——请求的主体php://input
不是 JSON。
我使用的代码与 Stripe 网站上的示例基本相同,对我来说效果很好:
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = Webhook::constructEvent(
$payload,
$sig_header,
$this->stripeApiKey
);
} catch (\UnexpectedValueException $e) {
throw new BadRequestHttpException('Unexpected value error');
} catch (SignatureVerification $e) {
throw new BadRequestHttpException('Signature verification error');
}
在此之后,您应该有一个有效的事件,$event
并且可以使用$object = $event->data->object
. 该对象将是什么类型取决于您收到 webhook 调用的事件。您可以使用 获取事件类型$event->type
。
(是的,我知道使用@
是不好的做法,但我并不介意那里的失败,因为一切都与是否Webhook::constructEvent()
有效有关。)
推荐阅读
- javascript - 如何使用多个按钮上传多个文件 - Google Apps 脚本
- microsoft-edge - 在 Edge 中下载时,西里尔文文件名转换为 =_UTF8?B?_[STRINGOFGIBBERISH]。如何解决?
- java - VDI 和 Java 规则集
- php - 绕过 php 7.2“字符串不支持 [] 运算符”或未初始化的变量?
- mysql - 无法再通过终端访问 MySQL
- python - 为什么 stat_density (R; ggplot2) 和 gaussian_kde (Python; scipy) 不同?
- jquery - 如何使用 jQuery 在 button > span 中找到类?
- typescript - firebase 和 typescript - '{}' 类型的参数不能分配给'Message' 类型的参数
- azure - 使用 Azure KeyVault 为连接字符串添加会话状态提供程序
- swift - 自调整大小集合视图单元格大小问题