symfony - 如何使用 Symfony HttpClient 获取所有中间重定向
问题描述
如何使用 HttpClient 获取所有重定向的 url?
这是一个例子:
- http://example.com/page 301 重定向到:
- http://www.example.com/page 302 重定向到:
- http://www.example.com/page/
$response = $this->httpClient->request('GET', 'http://example.com/page');
$content = $response->getContent(false);
$ary = $response->getInfo();
只有在 getInfo 的 '[debug]' 部分我可以看到:
Issue another request to this URL: 'http://www.example.com/page'
...
Issue another request to this URL: 'http://www.example.com/page/'
有没有办法在不解析调试文本的情况下检索重定向的 URL?
解决方案
您可以通过设置 max_redirects 参数来拦截重定向:
try {
$this->httpClient->request('GET', 'http://example.com/page', ['max_redirects' => 0])->getContent();
// not redirected
} catch (RedirectionException $e) {
$redirectUrl = $e->getResponse()->getInfo()['redirect_url'];
}
推荐阅读
- wordpress - 我的网站产品档案页面仅显示我正在使用商务的两个产品,并且所有设置都正常,如何解决这个问题?
- python - ImportError:无法导入名称 Person
- android - 如何在 2021 年使用融合的位置提供程序客户端快速且仅一次获取位置
- python - 在kivy中旋转后矩形变大
- node.js - 使用 node.js 的 vue 应用中的 PayFast notify_url 通知参数和安全检查失败
- docker - 通过 dockerfile 在 docker 中安装 conda 包
- reactjs - React - 如何引用 AntDesign Form.List 字段项
- npm - npm install 命令跳过 ssh 授权的包
- laravel - 如何在 laravel 中通过 jwt auth 保护网络路由?
- c - 为什么我没有收到来自服务器的消息?