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

lyonio's avatar

Get html content from auth protected route inside laravel app?

I try to get with curl (guzzle library) the html of current page to pass it to a pdf printer.

$html = new Client();
$html = $html->request('GET', $pdfPrintUrlContent)->getBody();

The problem is, all this routes are auth protected and I get there only html from my login page.

Is there any better way to get html from this protected route? With guzzle or any oher method...

0 likes
8 replies
Cronix's avatar

I think you just need to tell guzzle to use cookies so it can track the session cookie.

$client = new Client(['cookies' => true]);

// then send a post request to LOGIN with your credentials.
$client->post('/login', [
    'form_params' => [
        'username' => '[email protected]',
        'password' => 'yourPassword',
    ],
]);

// then send your get request once authenticated
$html = $client->request('GET', $pdfPrintUrlContent)->getBody();

// then logout
$client->get('/logout');

Should be close to that anyway

lyonio's avatar

is there not possible to give somehow session hash and use the same login?

crnkovic's avatar

What do you mean "HTML of the current page"? I'm not sure the context the Guzzle is put in. Where exactly is this code executed from? Separate app?

If your controller is rendering a view, you can get the HTML like this:

$view = view('my-template');
$html = $view->render();

// do something with $html

return $view;
Cronix's avatar

No, because it's based on the cookie, which is what laravel's auth is checking for so it needs to be present. No session cookie, no auth. So you just need to simulate what the browser is doing when you log in, and use cookies.

You could set up an api with laravel passport and use tokens and pass the token in the request, but that's overkill for this.

crnkovic's avatar

So... where exactly is Guzzled called from? I need more context.

If you're on PageA and trying to get HTML from the PageB, just execute the controller action that's loading PageB, or hardcode View that PageB controller is rendering.

If this is within one app, I am pretty sure Guzzle is not right way to do this. You have full control over your code and what is rendered where, yet you call Guzzle to make trip to your own server?

Cronix's avatar

That would have been better to start with lol. So just do as @crnkovic said and load the view file and just render it. Then use the rendered html how you want. You don't need guzzle for this at all.

$view = view('my-template');
$html = $view->render();

// now use $html for your pdf

You can send variables to the view if needed like normal.

Please or to participate in this conversation.