Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nipun's avatar
Level 2

Laravel Http client redirects to login page even after using cookies in subsequent requests - ChatGPT suggested approach already tried

I'm using Laravel Http client to scrape data from a website but to access the data I need to authenticate first. I was able to successfully authenticate using the code below:

$authResponse = Http::asForm()->post('https://www.example.com/login.php', [
    'email' => '[email protected]',
    'password' => '12345678',
]);

Next, I retrieved the cookies from the response using $authResponse->cookies(), and attempted to use them with headers in another request to get user details, but it just redirected me back to the login page:

$userDetailsResponse = Http::withHeaders([
    'Cookie' => $authResponse->cookies(),
])->get('https://www.example.com/profile.php');

However, I was able to accomplish this with cURL using this StackOverflow answer, but I'm interested in using the Laravel HTTP client instead. I know that for APIs we typically use a Bearer token with each authenticated request, but in this case, I'd like to use cookies.

I even asked about this from the ChatGPT, and it suggested the same approach I used. So, I'm hoping to get some fresh insights or guidance on how to accomplish this with the Laravel HTTP client.

Any advice or guidance would be greatly appreciated. Thank you!

0 likes
2 replies
LaryAI's avatar
Level 58

It's possible that the cookies are not being properly formatted when passed in the headers. Try converting the cookies array to a string using the http_build_query function before passing it in the headers. Here's an example:

$cookies = http_build_query($authResponse->cookies());

$userDetailsResponse = Http::withHeaders([
    'Cookie' => $cookies,
])->get('https://www.example.com/profile.php');

If that doesn't work, you can try manually setting the cookies in the client's cookie jar using the withCookie method. Here's an example:

$client = Http::withHeaders([
    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
])->withOptions([
    'cookies' => true,
]);

$authResponse = $client->asForm()->post('https://www.example.com/login.php', [
    'email' => '[email protected]',
    'password' => '12345678',
]);

$userDetailsResponse = $client->withCookie($authResponse->cookies())->get('https://www.example.com/profile.php');

This sets the cookies option to true when creating the client, which enables the cookie jar. Then, the withCookie method is used to manually set the cookies from the authentication response.

1 like
nipun's avatar
nipun
OP
Best Answer
Level 2

Thanks to @LaryAI I was able to fix this.

First, I used @LaryAI's reply, and then it was giving me the error

"cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface."

Then I updated my code below:

$client = Http::withHeaders([
    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
])->withOptions([
    'cookies' => $cookieJar,
]);

Then I received the error

"Method Illuminate\Http\Client\PendingRequest::withCookie does not exist."

To fix it, I updated my code as below:

foreach ($cookies as $cookie) {
    $cookieJar->setCookie($cookie);
}

$userDetailsResponse = $client->withOptions([
    'cookies' => $cookieJar,
])->get('https://www.example.com/profile.php');

After making these changes, everything started to work as required. Thanks again to @LaryAI.

Here is the full code:

use GuzzleHttp\Cookie\CookieJar;

$cookieJar = new CookieJar();

$client = Http::withHeaders([
    'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
])->withOptions([
    'cookies' => $cookieJar,
]);

$authResponse = $client->asForm()->post('https://www.example.com/login.php', [
    'email' => '[email protected]',
    'password' => '12345678',
]);

// Get the cookies as an array and add them to the cookie jar manually
$cookies = $authResponse->cookies();
foreach ($cookies as $cookie) {
    $cookieJar->setCookie($cookie);
}

$userDetailsResponse = $client->withOptions([
    'cookies' => $cookieJar,
])->get('https://www.example.com/profile.php');

Please or to participate in this conversation.