Http url link
1st. step: Disable https/ssl verify & set some headers …
$context = stream_context_create( [
	'ssl' => [
		'verify_peer' => false,
		'verify_peer_name' => false,
	],
	'http'=>array(
		'max_redirects'=>10,
		'ignore_errors'=>0,
		'method'=>"GET",
		'header'=>"Accept-language: en\r\n" .
		"Cookie: \r\n" .  
		"User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19\r\n" 
	)
]);
  • default method is GET, is not rejected by servers so is more safe to use than 'method' => 'HEAD'
  • we protect the script so as not to go into a very long execution: ‘max_redirects’=>10
  • set valid User-Agent browser , otherwise some sites restrict access!
2nd. step: get response headers
$headers = get_headers($url, 1, $context);
3rd. step: Get response code from header:
if(preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#", $headers[0] , $response ))
	$reponse_code = intval($response[1]);
else 
	$reponse_code = 0;
4rd. step: Parse result and get last location url:
if ($reponse_code == 200) 
	return $url;				
	
if (!isset($headers['Location']))
	$headers['Location'] = [$url];

$location = $headers['Location'];

if (!is_array($location))
	$location = [$location];

if ( ($reponse_code == 301) || ($reponse_code == 302) ) {	
	$last_location = $location[count($location)-1];		
	$httpfind = false;
	foreach (array_reverse($location) as $loc) {
		if (strpos($loc, 'http') !== FALSE) {
			$httpfind = true;
			if ($last_location != $loc) {
				$last_location = rtrim($loc,'/') .'/'. ltrim($last_location, '/');				
			}
			break;
		}
	}
	if (!$httpfind) {
		$last_location = rtrim($url,'/') .'/'. ltrim($last_location, '/');
	}
} else {
	$last_location = $fail;
}

And the final code used as a function:

function get_last_location($url, $fail='Invalid URL') {
	$context = stream_context_create( [
		'ssl' => [
			'verify_peer' => false,
			'verify_peer_name' => false,
		],
		'http'=>array(
			'max_redirects'=>10,
			'ignore_errors'=>0,
			'method'=>"GET",
			'header'=>"Accept-language: en\r\n" .
			"Cookie: \r\n" .  
			"User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19\r\n" 
		)
	]);	
	$headers = get_headers($url, 1, $context);	
	
	if(preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#", $headers[0] , $response ))
        $reponse_code = intval($response[1]);
	else 
		$reponse_code = 0;
	
	if ($reponse_code == 200) 
		return $url;				
		
	if (!isset($headers['Location']))
		$headers['Location'] = [$url];
	
	$location = $headers['Location'];
	
	if (!is_array($location))
		$location = [$location];

	if ( ($reponse_code == 301) || ($reponse_code == 302) ) {	
		$last_location = $location[count($location)-1];		
		$httpfind = false;
		foreach (array_reverse($location) as $loc) {
			if (strpos($loc, 'http') !== FALSE) {
				$httpfind = true;
				if ($last_location != $loc) {
					$last_location = rtrim($loc,'/') .'/'. ltrim($last_location, '/');				
				}
				break;
			}
		}
		if (!$httpfind) {
			$last_location = rtrim($url,'/') .'/'. ltrim($last_location, '/');
		}
	} else {
		$last_location = $fail;
	}
	
	return $last_location;	
}

Happy Coding !

Tags: , , , , , ,

Leave a Reply

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