0
votes

Guzzle6 has one pretty option: allow_redirects. If setting this option to false value, Guzzle blocking redirect if response code like 302 or Headers has Location param.

But there is one problem. If redirect sends by client – Guzzle doesn't notice this. For example:

We have some URL http://example.com/ that redirected to page http://redirected.com/. Send request with help Guzzle:

$oClient->request('POST', 'http://redirected.com/', [
    'form_params' => $aFormData,
    'cookies' => (new CookieJar($aPrepareCookie)),
    'debug' => true,
    'allow_redirects' => false
]);

Suppose that redirect.com has index.php like this:

<?php
    header('Location: http://www.example.com/');
?>

In this case, the redirection will be blocked from Guzzle.

Now, let's see this redirect example:

<?php
    echo '<script>window.location = "http://www.example.com/";</script>';
?>

Here we have a problem because this is client side redirect and his can't affect the Headers, Therefore Guzzle can't track HTTP Status Code or Location value.

So, how i can solve this problem and find solution?

Thanks for help!

1
guzzle doesn't execute javascript, it just performs the HTTP request - your problem has to be solved downstream since it resides in what you're doing with the request content. - Calimero
@Calimero you offer to get a page on which there is the window.location and cut this, for example. But, a can't get page because immediately executed redirect. - RomanGor
I suspect you're performing a request with guzzle in a webpage and sending back the request's content to the client - only a web browser will execute a JS redirect. Guzzle being only an HTTP client, it's out of its responsibilities to handle this for you. but you easily can filter the response content if needed. - Calimero

1 Answers

1
votes

As previous posters have said, Guzzle will not execute the javascript.

Your problem could easily be solved by utilizing Guzzle Middleware.

Within the middleware:

  • receive the response
  • parse the body for window.location="someurl"
  • perform a request of "someurl"
  • modify the header of the response from "someurl" to include the fact that it was a redirection.
  • return the response.