Reputation: 11
I want to get final redirected url after all redirection in php. I used lots of function and code searched from web but no one is correct for some tracking url.
Here is my url : http://performance.ikoo.com/geo_tracking_redirect.html?a=CD441&program_id=1304274
It finally redirects to : https://www.shopandship.com/my-shopandship/profile.aspx
How can i get the final redirect url for such type of url. please help.
Upvotes: 1
Views: 1992
Reputation: 152
Goutte can do this for you. https://github.com/FriendsOfPHP/Goutte
It will follow HTTP 3xx redirects and also <meta http-equiv="refresh">
tags.
Sample code:
$client= new \Goutte\Client();
$url="http://www.awin1.com/awclick.php?mid=5477&id=129196";
echo $client->request("GET",$url)->getUri();
Upvotes: 0
Reputation: 80
you can try this code for this url..
Which returns redirected url for tracking url..
$url = "http://www.awin1.com/awclick.php?mid=5477&id=129196";
stream_context_set_default(array('http' => array('method' => 'HEAD')));
print_r(get_headers($url, 0));
$larr = get_headers($url, 1)["Location"];
$loc = is_array($larr) ? array_pop($larr):$larr;
echo $loc."</pre>";
$domain = parse_url($loc, PHP_URL_HOST);
print_r($domain);
Upvotes: 0
Reputation: 5689
Just Try with this.
<?php
function getLastEffectiveUrl($url)
{
// initialize cURL
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
));
// execute the request
$result = curl_exec($curl);
// extract the target url
$redirectUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_close($curl);
return $redirectUrl;
}
$url="http://performance.ikoo.com/geo_tracking_redirect.html?a=CD441&program_id=1304274";
$lastEffectiveUrl = getLastEffectiveUrl($url);
if($lastEffectiveUrl==$url)
{
$url_data=file_get_contents($url);
$domHtmlStr = new DOMDocument();
$domHtmlStr->loadHTML($url_data);
$metaTag = $domHtmlStr->getElementsByTagName('meta');
foreach($metaTag as $metaTagVal)
{
if($metaTagVal->getAttribute("http-equiv")=="refresh")
{
$metaContent = $metaTagVal->getAttribute("content");
break;
}
}
if($metaContent!="")
{
$metaContentSplit=explode("URL=",$metaContent);
$lastEffectiveUrl1 = getLastEffectiveUrl(trim($metaContentSplit[1]));
echo "Final URL : ".$lastEffectiveUrl1;
}
}
?>
Source : http://codeaid.net/php/get-the-last-effective-url-from-a-series-of-redirects-for-the-given-url
Note : http://performance.ikoo.com/geo_tracking_redirect.html?a=CD441&program_id=1304274 is not loading!
Upvotes: 3