我尝试在PhpUnit中的WebTestCase中发送原始数据但它不起作用:
$jsonEvent = '{
"type": "invoice.payment_succeeded",
}';
$this->client->request(
'POST',
'/api/v1/stripe/webhook',
[],
[],
['CONTENT_TYPE' => 'application/json'],
$jsonEvent
);
我尝试获取这样的数据:
$input = file_get_contents("php://input");
var_dump($input);
但$input
为空
不确定但也许不可能在webtestcase中获得类似的内容输入?
提前致谢。
答案 0 :(得分:3)
我假设您使用的是 Symfony 的 WebTestCase 。如果是这种情况,那么首先,从json中删除无效的逗号:
$jsonEvent = '{
"type": "invoice.payment_succeeded"
}';
其次,我假设您正在以这种方式创建客户端:
$this->client = static::createClient();
如果是这种情况,则问题是请求方法没有执行http请求,但它正在查找控制器和操作(内部使用&#34; Symfony \ Component \ HttpKernel \ Controller \ ControllerResolver&#34; )并调用它将请求方法中设置的参数,内容和标题传递给相应的symfony对象。< / p>
这就是你不能使用的原因:
$input = file_get_contents("php://input");
因为你真的在运行相同的脚本。
要在您的操作中获取帖子正文,一种方法是:
public function myAction(Request $request)
{
$input = $request->getContent();
参考文献&amp;实例强>