【问题标题】:How can I access the URL of a Symfony Http Client response?如何访问 Symfony Http 客户端响应的 URL?
【发布时间】:2021-06-09 09:31:33
【问题描述】:

使用 Symfony Http 客户端,我创建了一个这样的 HTTP 请求:

 $client = HttpClient::create();

 $response = $client->request('GET', 'www.mypage.com', [
 'auth_basic' => ['user', '123'],
 ]);

 dump($response);

输出是:

Symfony\Component\HttpClient\Response\CurlResponse {#1117 ▼
  response_headers: []
  http_code: 0
  error: null
  url: www.something.com
  ....

现在我尝试输出该响应的url

 dump($response->getContent());

但输出是null

我怎样才能得到 url www.something.com 的输出?

【问题讨论】:

  • 似乎getInfo 将是返回url 的方法。这是你要找的吗?

标签: php symfony symfony-http-client


【解决方案1】:

所有HttpClient 响应都实现了ResponseInterface,在 Symfony 合约中定义。

该接口声明了一个getInfo() 方法,如here 所示。

该方法返回一个数组,响应的有效URL存储在url键处。

$finalUrl = $response->getInfo()['url'];

并不是说如果请求经过一个或多个 HTTP 重定向,最终解析的 URL 可能与最初请求的 URL 不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2017-02-23
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多