简体   繁体   English

完全禁用Guzzle的重定向

[英]Completely disable redirection for Guzzle

Guzzle6 has one pretty option: allow_redirects . Guzzle6有一个不错的选择: allow_redirects If setting this option to false value, Guzzle blocking redirect if response code like 302 or Headers has Location param. 如果将此选项设置为false值,则在响应代码(例如302Headers具有Location参数的情况下,Guzzle将阻止重定向。

But there is one problem. 但是有一个问题。 If redirect sends by client – Guzzle doesn't notice this. 如果重定向是由客户端发送的,则Guzzle不会注意到这一点。 For example: 例如:

We have some URL http://example.com/ that redirected to page http://redirected.com/ . 我们有一些URL http://example.com/重定向到页面http://redirected.com/ Send request with help Guzzle: 发送请求与帮助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: 假设redirect.com具有如下的index.php

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

In this case, the redirection will be blocked from Guzzle. 在这种情况下,重定向将不受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. 这里我们有一个问题,因为这是客户端重定向,并且他不能影响Headers ,因此Guzzle无法跟踪HTTP状态代码或位置值。

So, how i can solve this problem and find solution? 那么,我如何解决这个问题并找到解决方案?

Thanks for help! 感谢帮助!

As previous posters have said, Guzzle will not execute the javascript. 正如以前的海报所说,Guzzle不会执行javascript。

Your problem could easily be solved by utilizing Guzzle Middleware . 您可以通过使用Guzzle中间件轻松解决您的问题。

Within the middleware: 在中间件中:

  • receive the response 收到回应
  • parse the body for window.location="someurl" 解析主体以获取window.location =“ someurl”
  • perform a request of "someurl" 执行“ someurl”请求
  • modify the header of the response from "someurl" to include the fact that it was a redirection. 从“ someurl”修改响应的标题,以包含响应是重定向的事实。
  • return the response. 返回响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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