SOLVED: Yii2: How to allow Guzzle POST requests in a controller - TagMerge
4SOLVED: Yii2: How to allow Guzzle POST requests in a controllerSOLVED: Yii2: How to allow Guzzle POST requests in a controller

SOLVED: Yii2: How to allow Guzzle POST requests in a controller

Asked 1 years ago
1
4 answers

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.

Source: link

0

use GuzzleHttp\Client;

$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'http://httpbin.org',
    // You can set any number of default request options.
    'timeout'  => 2.0,
]);
// Create a client with a base URI
$client = new GuzzleHttp\Client(['base_uri' => 'https://foo.com/api/']);
// Send a request to https://foo.com/api/test
$response = $client->request('GET', 'test');
// Send a request to https://foo.com/root
$response = $client->request('GET', '/root');
$response = $client->get('http://httpbin.org/get');
$response = $client->delete('http://httpbin.org/delete');
$response = $client->head('http://httpbin.org/get');
$response = $client->options('http://httpbin.org/get');
$response = $client->patch('http://httpbin.org/patch');
$response = $client->post('http://httpbin.org/post');
$response = $client->put('http://httpbin.org/put');
use GuzzleHttp\Psr7\Request;

$request = new Request('PUT', 'http://httpbin.org/put');
$response = $client->send($request, ['timeout' => 2]);
$promise = $client->getAsync('http://httpbin.org/get');
$promise = $client->deleteAsync('http://httpbin.org/delete');
$promise = $client->headAsync('http://httpbin.org/get');
$promise = $client->optionsAsync('http://httpbin.org/get');
$promise = $client->patchAsync('http://httpbin.org/patch');
$promise = $client->postAsync('http://httpbin.org/post');
$promise = $client->putAsync('http://httpbin.org/put');

Source: link

0

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';
   }
}
SOLVED: The problem is that Yii expects a CSRF token so I had to disable it:
public function beforeAction($action) {
    $this->enableCsrfValidation = false;
        
    return parent::beforeAction($action);
}

Source: link

0

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';
   }
}
SOLVED: The problem is that Yii expects a CSRF token so I had to disable it:
public function beforeAction($action) {
    $this->enableCsrfValidation = false;
        
    return parent::beforeAction($action);
}

Source: link

Recent Questions on yii

    Programming Languages