我正在尝试获取网站的favicon图像路径,我正在做类似的事情:
$favicon_img_url = $link->getAttribute('href');
echo $favicon_img_url;
但它只返回我想要的相对URL(/favicon.ico)而不是绝对链接(http://www.anysite.com/favicon.ico)。
编辑:为了更清晰,这里有更大的代码块:
function file_get_contents_curl($url)
{
//Some code here to get contents from a website....
}
$html = file_get_contents_curl($target_website);
$doc = new DOMDocument();
@$doc->loadHTML($html);
// GET FAVICON PATH
$links = $doc->getElementsByTagName('link');
for ($i = 0; $i < $links->length; $i++)
{
$link = $links->item($i);
$rel = $link->getAttribute('rel');
if($rel == 'shortcut icon')
$favicon = $link->getAttribute('href');
}
echo $favicon;
它只返回“/favicon.ico”而不是“http://www.website.com/favicon.ico”
答案 0 :(得分:0)
尝试添加到相对路径,域url $ _SERVER ['SERVER_NAME']
编辑:
它为您提供没有http或https的域名。要添加它,您可以检查它是https还是http:if(isset($_SERVER['HTTPS'])){..}else{...}
答案 1 :(得分:0)
href
属性被定义为(绝对/相对)路径并保持这种方式。当浏览器处理HTML标记时,它将使用该属性执行以下两项操作之一:
href
属性。href
属性。以此页面标题为例,如果您检查标记,则属性为
/questions/26441184/php-how-to-get-absolute-link-of-href-attribute
这不是绝对路径,但由于浏览器知道基本网址为http://stackoverflow.com,因此点击它会转到属性值,即:
http://stackoverflow.com/questions/26441184/php-how-to-get-absolute-link-of-href-attribute
tl; dr :您不能不知道<a>
所在网页的基本网址。