Skip to content
Advertisement

How can I access the URL of a Symfony Http Client response?

With the Symfony Http Client I create a HTTP request like this:

 $client = HttpClient::create();

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

 dump($response);

The output is:

SymfonyComponentHttpClientResponseCurlResponse {#1117 ▼
  response_headers: []
  http_code: 0
  error: null
  url: www.something.com
  ....

Now I try to output the url of that response:

 dump($response->getContent());

But the output is null.

How can I get as an output the url www.something.com?

Advertisement

Answer

All the HttpClient responses implement the ResponseInterface, defined on Symfony Contracts.

That interface declares a getInfo() method, as shown here.

This method returns an array, and the effective URL of the response is stored at url key.

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

Not that if the request went through one or more HTTP redirects, the final resolved URL may not be the same as the one originally requested.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement