简体   繁体   English

yii2 Webhook发布空

[英]yii2 Webhook post empty

I am kinda new to the yii FrameWork, and i need help. 我是yii FrameWork的新手,我需要帮助。

i need to implement a stripe webhook controller that is used for the subscription event sent by Stripe. 我需要实现一个条带webhook控制器,用于Stripe发送的订阅事件。 For this controller, there is no view nor model 对于此控制器,没有视图或模型

I can access to the controller, but the $_POST content is empty and i cannot figure why. 我可以访问控制器,但$ _POST内容为空,我无法理解为什么。

Is it possible to use the post verb without a view ? 是否可以在没有视图的情况下使用动词?

here's an example : 这是一个例子:

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...
}

i used the 我用过

 print_r(Yii::$app->request->post() /*$_POST*/); exit(); 

and i only got an empty array. 我只有一个空数组。

After days of search i found nothing... 经过几天的搜索,我什么都没发现......

If any one has an idea, i will gladly take it 如果有人有想法,我会乐意接受

Additionnal info : we are running on a IIS web server, using the Yii2 framework 附加信息:我们使用Yii2框架在IIS Web服务器上运行

Thanks for reading me cya 谢谢你读我的cya

For anyone similarly stuck, I had to do the following to get this working: 对于任何类似卡住的人,我必须执行以下操作才能使其正常工作:

Disable CSRF protection for the whole class 禁用整个班级的CSRF保护

public $enableCsrfValidation = false;

Use getRawBody() instead of post() 使用getRawBody()而不是post()

$data = json_decode(Yii::$app->request->getRawBody());

Outside the framework you would use file_get_contents("php://input") 在框架之外,你将使用file_get_contents("php://input")

Avoid any server side redirects 避免任何服务器端重定向

In my case that meant no trailing slash or www. 在我的情况下,这意味着没有尾随斜线或www。 in the URL 在URL中

If Yii::$app->request->post() is empty then the request has not POST data. 如果Yii::$app->request->post()为空,则请求没有POST数据。 Grab the request in beforeAction and dump the entire thing. 在beforeAction中获取请求并转储整个事件。 That will be what your machine is receiving. 这将是您的机器正在接收的。 If empty, the machine is not receiving the data being sent with the request. 如果为空,则机器不会接收随请求一起发送的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM