yii2 Webhook 帖子为空 [英] yii2 Webhook post empty
问题描述
我对 yii FrameWork 有点陌生,我需要帮助.
I am kinda new to the yii FrameWork, and i need help.
我需要实现一个 Stripe webhook 控制器,用于 Stripe 发送的订阅事件.对于这个控制器,没有视图和模型
i need to implement a stripe webhook controller that is used for the subscription event sent by Stripe. For this controller, there is no view nor model
我可以访问控制器,但 $_POST 内容为空,我不知道为什么.
I can access to the controller, but the $_POST content is empty and i cannot figure why.
是否可以在没有视图的情况下使用后置动词?
Is it possible to use the post verb without a view ?
这是一个例子:
class StripeWebhookController extends Controller
{
public function beforeAction($action)
{
if ($action->id == 'index') {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
public function actionIndex()
{
header('Content-Type: text/html; charset=utf-8');
StripeLoader::autoload();
\Stripe\Stripe::setApiKey( Settings::get("stripe_secret_key") );
// retrieve the request's body and parse it as JSON
$input = file_get_contents('php://input'); // -> here $input is null
$event_json = json_decode($input, true);
// Do the work...
}
我用过
print_r(Yii::$app->request->post() /*$_POST*/); exit();
而我只有一个空数组.
经过几天的搜索,我一无所获......
After days of search i found nothing...
如果有人有想法,我很乐意接受
If any one has an idea, i will gladly take it
附加信息:我们在 IIS Web 服务器上运行,使用 Yii2 框架
Additionnal info : we are running on a IIS web server, using the Yii2 framework
感谢阅读我青
推荐答案
对于任何类似问题的人,我必须执行以下操作才能使其正常工作:
For anyone similarly stuck, I had to do the following to get this working:
为全班禁用CSRF保护
public $enableCsrfValidation = false;
使用 getRawBody() 而不是 post()
$data = json_decode(Yii::$app->request->getRawBody());
在框架之外,您将使用 file_get_contents("php://input")
Outside the framework you would use file_get_contents("php://input")
避免任何服务器端重定向
就我而言,这意味着没有尾部斜杠或 www.在网址中
In my case that meant no trailing slash or www. in the URL
这篇关于yii2 Webhook 帖子为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!