我想获取重定向网址中的最终网址。 我有这个功能:
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;
}
但是它不适用于所有链接,有些链接是:
https://www.goldpoll.com/out.php?p=28017
https://investlister.com/go/lid/68/
答案 0 :(得分:0)
<?php
$target = 'https://investlister.com/go/lid/68/';
$website_host_url = str_ireplace('www.', '', parse_url($target)['host']);
echo $website_host_url; // investlister.com
这完全符合您的预期
答案 1 :(得分:0)
好时间; 您需要访问带有https的curl函数 测试一下:
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;
}