get redirect url in php

How to Get Redirect URL in PHP using cURL?

There are a lot of projects in which we need to get a redirect URL in PHP. Like if we have a shortened URL and if we want to know what’s the URL behind it then cURL is one the best option that can help you in this case.

Let’s create a function that will return a redirected URL in response.

function getRedirect($url){
	// Initialize the cURL
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // This is the Redirected URL you are looking for
	$RedirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 


	return $RedirectedUrl; 

Now, this function will help you get a redirected URL. Let’s execute this function.

echo getRedirect('');

Now, the output is

I have tried my best to guide you that how to get the redirected URL in PHP using cURL. If you have any type of questions, you can comment below and I will try to answer your each and every question.

Give a Comment