To get the destination URL using cURL in PHP, you can use the CURLOPT_FOLLOWLOCATION option, which will follow any redirects and update the CURLOPT_URL option with the final URL.
Here's an example:
$url = "https://www.example.com";
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url); // initial URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return response as string
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects
// Execute cURL session
$response = curl_exec($ch);
// Check for errors
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
// Get the final URL after following redirects
$finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
// Close cURL session
curl_close($ch);
// Process the response and final URL
echo "Response: " . $response . "<br>";
echo "Final URL: " . $finalUrl;
In this example, we set the initial URL using the CURLOPT_URL option and enable the CURLOPT_FOLLOWLOCATION option to follow any redirects. After executing the cURL session, we get the final URL using the curl_getinfo () function with the CURLINFO_EFFECTIVE_URL option. This will return the final URL after following any redirects.
Note that if there are no redirects, the final URL will be the same as the initial URL.