misagh ahmadi
misagh ahmadi

Reputation: 89

final url in redirects

I want to get final url in redirecting urls . I have this function :

    function get_redirect_final_host_url($url){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect
    curl_exec($ch);
    $target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
$website_host_url = str_ireplace('www.', '', parse_url($target)['host']);
return $website_host_url;
}

but it doesn`t work on all links , some links are :

https://www.goldpoll.com/out.php?p=28017
https://investlister.com/go/lid/68/

Upvotes: 1

Views: 67

Answers (2)

Ghasminia
Ghasminia

Reputation: 26

Good time ; You need to access the curl function for those that have https Test it:

function get_redirect_final_host_url($url){
    $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow redirects
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // set referer on redirect
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // get access ssl
        curl_exec($ch);
        $target = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    $website_host_url = str_ireplace('www.', '', parse_url($target)['host']);
    return $website_host_url;
}

Upvotes: 1

<?php
$target = 'https://investlister.com/go/lid/68/';
$website_host_url = str_ireplace('www.', '', parse_url($target)['host']);

echo $website_host_url; // investlister.com

This works exactly as you would expect

Upvotes: 0

Related Questions