简体   繁体   中英

Completely disable redirection for Guzzle

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM