简体   繁体   English

PHP:如何获取href属性的绝对链接

[英]PHP: How to get absolute link of href attribute

I'm trying to fetch favicon image path of a site and I'm doing something like: 我正在尝试获取网站的网站图标图像路径,并且正在执行以下操作:

$favicon_img_url = $link->getAttribute('href');
echo $favicon_img_url;

But it only returns the relative url (/favicon.ico) and NOT the absolute link ( http://www.anysite.com/favicon.ico ) that I want. 但是它只返回我想要的相对URL(/favicon.ico),而不返回绝对链接( http://www.anysite.com/favicon.ico )。

EDIT: For more clarity, here's a bigger chunk of the code: 编辑:为了更清楚,这是代码的更大块:

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; 

It only returns "/favicon.ico" not " http://www.website.com/favicon.ico " 它仅返回“ /favicon.ico”,而不返回“ http://www.website.com/favicon.ico

try add to relative path, domain url $_SERVER['SERVER_NAME'] 尝试添加到相对路径,域网址$ _SERVER ['SERVER_NAME']

edit: it give you domain without http or https. 编辑:它为您提供没有http或https的域。 To add it, you can check if it is https or http: if(isset($_SERVER['HTTPS'])){..}else{...} 要添加它,您可以检查它是https还是http: if(isset($_SERVER['HTTPS'])){..}else{...}

The href attribute is defined as an (absolute/relative) path and stays that way. href属性定义为(绝对/相对)路径,并保持这种状态。 When the browser processes the HTML markup, it will do one of two things with that attribute: 当浏览器处理HTML标记时,它将使用该属性执行以下两项操作之一:

  • If it considers the attribute a valid absolute path, that will become that elements href property. 如果它认为该属性为有效的绝对路径,则它将成为该元素的href属性。
  • If it considers the attribute a relative path, it will pop it on the end of the base URL and use that as the href property. 如果将属性视为相对路径,则会将其弹出到基本URL的末尾,并将其用作href属性。

Take this page title for example, if you inspect the markup, the attribute is 以该页面标题为例,如果您检查标记,则属性为

/questions/26441184/php-how-to-get-absolute-link-of-href-attribute

It is not an absolute path, but because the browser knows that the base URL is http://stackoverflow.com , clicking it will take you to the property value, which is: 它不是绝对路径,但是由于浏览器知道基本URL是http://stackoverflow.com ,因此单击它会将您带到属性值,即:

http://stackoverflow.com/questions/26441184/php-how-to-get-absolute-link-of-href-attribute

tl;dr : You can't, not without knowing the base URL for the page on which your <a> falls. tl; dr :不能不知道<a>所在页面的基本URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM