Search code examples
yiiyii2guzzleyii2-advanced-appbad-request

Yii2: How to allow Guzzle POST requests in a controller


I’m trying to make a POST request via Guzzle to a Yii controller but getting a "Bad Request #400". I thought when I don’t use behaviours() the controller is automatically accessible to all kinds of requests, but nope. How I can solve this? What would be best practice for CURL/Guzzle requests in Yii2?

class ImportController extends yii\web\Controller {
   public function actionIndex() {
      return 'OK';
   }
}

Solution

  • You should create a rest controller instance (yii\rest\ActiveController) and implement authentication for it as described here: https://www.yiiframework.com/doc/guide/2.0/en/rest-authentication

    That is probably the correct approach to your use case, and you would not have to deal with CSRF.