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.

$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.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information