Reputation: 43
I am kinda new to the yii FrameWork, and i need help.
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
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 ?
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
Thanks for reading me cya
Upvotes: 4
Views: 3010
Reputation: 1038
For anyone similarly stuck, I had to do the following to get this working:
Disable CSRF protection for the whole class
public $enableCsrfValidation = false;
Use getRawBody() instead of post()
$data = json_decode(Yii::$app->request->getRawBody());
Outside the framework you would use file_get_contents("php://input")
Avoid any server side redirects
In my case that meant no trailing slash or www. in the URL
Upvotes: 7
Reputation: 2037
If Yii::$app->request->post()
is empty then the request has not POST data. Grab the request in beforeAction and dump the entire thing. That will be what your machine is receiving. If empty, the machine is not receiving the data being sent with the request.
Upvotes: 0