I need to send a request with custom cookies.
I have tried to set cookieJar like this:
$cookieJar = CookieJar::fromArray(array($cookieName=>$cookieStr), 'api.mobra.in'); $res = $this->guzzleClient->request($requestMethod, $url, [ 'cookies' => [$cookieJar] ] );
But it is getting an error
cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface
Please suggest example or explain in details. I gone through documents but they have not mentioned in detail.
Thank you!
Guzzle can maintain a cookie session for you if instructed using the cookies request option. When sending a request, the cookies option must be set to an instance of GuzzleHttp\Cookie\CookieJarInterface .
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...
You can check to see if a request or response has a body using the getBody() method: $response = GuzzleHttp\get('http://httpbin.org/get'); if ($response->getBody()) { echo $response->getBody(); // JSON string: { ... } }
use GuzzleHttp\Cookie\CookieJar; $cookieJar = CookieJar::fromArray([ 'cookie_name' => 'cookie_value' ], 'example.com'); $client->request('GET', '/get', ['cookies' => $cookieJar]);
You can read the documentation here.
One more way to add a cookie to the request with Guzzle:
$url = 'https://www.example.com'; $request_options = [ 'headers' => ['Cookie' => 'COOKIE_NAME=VALUE'] ]; $response = $this->httpClient->request('GET', $url, $request_options);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With