How to read the response effective URL in Guzzle ~6.0
If you are migrating from an older version of Guzzle to the 6.x series, one of the most immediate friction points is how redirects are handled and exposed. In previous iterations of the library, you could simply call $response->getEffectiveUrl() on a Guzzle\Http\Response object and receive the final destination URI. However, the architecture shifted significantly in version 6.0 to align with the PSR-7 standard. Consequently, the Response class returned by the client is now an implementation of Psr7\Response, which does not include a getEffectiveUrl() method out of the box.
This change forces developers to look at the redirect history rather than relying on a single property on the final response object. When you follow redirects using Guzzle's default behavior, the client creates a new history for every hop. The final response object only contains the URI of the last request that was actually made, not necessarily the one your original code sent.
To retrieve the effective URL in Guzzle 6.0, you need to inspect the redirect history associated with the transaction. The ResponseInterface interface itself does not define a method for this, but the specific implementation used by Guzzle stores the history in an object accessible via a property. You can access the underlying history through the getRedirectionHistory() method if available, or more commonly by accessing the private _redirectHeaders or similar internal structures depending on the exact patch version, though the most robust way is to check the history of the request itself.
The cleanest approach involves checking the request history attached to the response object. You can access this via $response->getTransferStats()->getEffectiveUrl() in some configurations, but strictly speaking, you often need to look at the redirect history directly. In Guzzle 6.x, the Response object has a property that holds the history of redirects. However, the most reliable pattern involves checking the TransferStats object attached to the response.
Here is a practical example of how to access the effective URL in a Guzzle 6 setup:
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$client = new Client([
'timeout' => 10.0,
]);
try {
$response = $client->post('http://service.com/login', [
'form_params' => [
'user' => $user,
'pass' => $pass,
],
// Ensure redirects are followed to populate history
'allow_redirects' => true
]);
// The effective URL is often found in the transfer stats or by inspecting the redirect history.
// In Guzzle 6, you can access the final URI via the response headers or stats depending on configuration.
// However, the most direct way to get the final URL after redirects is:
$effectiveUrl = null;
if (isset($response->getHeaders()['Location'][0])) {
// This might be the last redirect header, not necessarily the final URL if max redirects were hit.
}
// A more robust check uses the TransferStats which Guzzle 6 populates when following redirects.
$stats = $response->getTransferStats();
// Depending on the exact sub-version of Guzzle 6, accessing the effective URI
// might require checking the history array directly if getEffectiveUrl isn't exposed via interface.
// But typically, if redirects were followed, the response body and headers belong to the final URL.
// If you specifically need the string representing the final location:
$finalUri = $stats->getEffectiveUrl() ?? $response->getUri();
} catch (RequestException $e) {
// Handle exceptions where redirects might have failed or been forbidden.
}
It is important to note that if you disable redirect following, the response object will correspond to the 301/302 status code received initially, not the final resource. In such cases, $response->getUri() returns the original target unless you explicitly follow the jump. The TransferStats object is the key differentiator here; it captures metadata about the entire transaction, including the effective URI if the client automatically resolved redirects.
The documentation available in Guzzle 6 can sometimes be sparse regarding these internal properties because they are part of the PSR-7 abstraction layer which intentionally omits them to keep the interface clean. This is a common pitfall when upgrading: assuming that all convenience methods from the old Guzzle\Http namespace map directly to the new Psr7 objects. They do not, and understanding where data is stored (often in the TransferStats or via reflection on the history object) is crucial for debugging redirect chains.
If you require a solution that works regardless of Guzzle's internal implementation details, consider explicitly handling the redirect logic yourself by disabling automatic redirects and reading the Location header. This gives you full control over when and how to update your current URI, but it shifts the complexity from the library to your business logic.