Retrieve Final URL after Curl Redirect
If you are using cURL to retrieve a webpage and the page redirects to a different URL, you may want to retrieve the final URL after the redirect. You can achieve this by using the CURLOPT_FOLLOWLOCATION option in cURL.
First, you need to initialize cURL and set the CURLOPT_FOLLOWLOCATION option to true. This will allow cURL to follow any redirects that occur.
$ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Next, you can make the request and retrieve the final URL using the curl_getinfo function. This function returns an array of information about the request, including the final URL.
curl_exec($ch); $final_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
Now you have the final URL after any redirects that occurred during the request. This can be useful if you need to perform additional actions based on the final URL, such as parsing the content of the page or making another request.
In summary, to retrieve the final URL after a cURL redirect, initialize cURL with the CURLOPT_FOLLOWLOCATION option set to true, make the request, and retrieve the final URL using the curl_getinfo function.
Leave a Reply