guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface

kevalsavani opened this issue · comments

Guzzle version affected: ^7.2
PHP version: x.y.z (hint: 8.1.6)
cURL version: x.y.z (hint: 7.83.1)

Description
I am using Laravel version ^9.11 where I need to call third party API to fetch/post request using cookie.
After 1 day my cookies expires and need to retrieve the cookie by calling specific API call which will provide me new cookie to set in current call to avoid any failure, below is the code where I create my Guzzle instance

$coockie = ['Cookie' => "ab-experiment-cl=true;connect.sid=s%3A3tHhCF3Qank_kdRMyOHgcB6fNn_Kb3lL.H%2BpP8bdbsdasdfasfHJlKS3FNbgdyMPBEKrbxhR0Bu6XLIv97MVI;__cf_bm=btMzy6q8cvv58AP8oDVbLJQhNvB1_AtezaHLFAtPhY4-1658343124-0-AWMrzMkFMSDtXmuzxF4HlMPgSYck742Lqa90HRTIiWukvYORqf5BdcBobL6A3yaDeUHYevB12DB1/XCbh5HSLus="];

// Prepare the HTTP request
$this->http = Http::withHeaders(array_merge([
	"authority" => "xxxxx.xxxxx.com",
	"accept-language" => "en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7",
	"origin" => "https://xxxxx.xxxx.com",
	"referer" => "https://xxxx.xxxx.com",
]))->withCookies($cookies, "xxxx.xxxx.com")->retry(2, 5000, function ($exception, $request) {

	// Retrieve new cookie
	if ($exception->response->status() === 401) {
		$cookieString = $this->refreshSession(); // Calling login API again to get the new cookies
		$request->withCookies($cookieString, "xxxx.xxxx.com");
		return true;
	}
})->baseUrl("https://xxxx.xxxx.com/{$apiVersion}api")->timeout(5); 


function refreshSession(){
    // Case 1
    // With Cookie String (Calling an Login API to fetch the cookie)
    // Manipulating the cookie and prepare the string to pass
    return ['Cookie' => "ab-experiment-cl=true;connect.sid=s%3AbfLvXqfieVlYnAG4tBh_94M8jIFVxHFV.uzUGg6oiLQphDM9wVa9IDbisp9wmaCmzlYlH5VYiaVs;__cf_bm=ITq3nh3fB677X9sPkaaFKnSF9qrzRH_8mOCKqAw2tiA-1658375338-0-AajT2ISJdJOWNkDUHp+4Ygml0tP+rpqN1P4/0/ljK0PC2vaelSHhNvKa+Zn9F3HB1Nkp5NC6rvdeT7UFsDdHRRY="];


    // Case 2
    // With `\GuzzleHttp\Cookie\CookieJar` and `\GuzzleHttp\Cookie\SetCookie`
    $jar = new \GuzzleHttp\Cookie\CookieJar();
        foreach ($cookies as $cookieValue) {
	    $jar->setCookie(new \GuzzleHttp\Cookie\SetCookie($cookieValue));
        }
    }
    return ['Cookie' => $jar];
}

Additional context
Basically I am handling 401 in retrie method of HTTP request to make client request auto.

In Case 1 I am getting an error like cookies must be an instance of GuzzleHttp\\Cookie\\CookieJarInterface so I tried with Case 2

In Case 2 After setting the CookieJar I still get the same issue cookies must be an instance of GuzzleHttp\\Cookie\\CookieJarInterface

What I am missing here? Please guide through.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.