php将所有链接转换为绝对URL

php将所有链接转换为绝对URL,php,url,curl,hyperlink,web-crawler,Php,Url,Curl,Hyperlink,Web Crawler,我正在用php编写一个网站爬虫,我已经有了可以从一个网站中提取所有链接的代码。 一个问题:站点使用绝对URL和相对URL的组合。 示例(http替换为hxxp,因为我无法发布超链接): hxxp://site.com/ site.com site.com/index.php hxxp://site.com/hello/index.php /hello/index.php hxxp://site2.com/index.php site2.com/index.php 我无法控制链接(如果它们是绝对/

我正在用php编写一个网站爬虫,我已经有了可以从一个网站中提取所有链接的代码。 一个问题:站点使用绝对URL和相对URL的组合。 示例(http替换为hxxp,因为我无法发布超链接):

hxxp://site.com/

site.com

site.com/index.php

hxxp://site.com/hello/index.php

/hello/index.php

hxxp://site2.com/index.php

site2.com/index.php

我无法控制链接(如果它们是绝对/相对的),但我确实需要遵循它们。我需要将所有这些链接转换为绝对URL。如何在php中实现这一点?

这是一个开始

// Your crawler was sent to this page.
$url = 'http://example.com/page';

// Example of a relative link of the page above.
$relative = '/hello/index.php';

// Parse the URL the crawler was sent to.
$url = parse_url($url);

if(FALSE === filter_var($relative, FILTER_VALIDATE_URL))
{
    // If the link isn't a valid URL then assume it's relative and
    // construct an absolute URL.
    print $url['scheme'].'://'.$url['host'].'/'.ltrim($relative, '/');
}

作为创建绝对锚定的另一种方式,请查看该方法。

您使用什么来解析html和查找链接?您的库可能已经有了解析相对URL的方法。我正在使用自己的html链接提取功能。除了curl和php函数之外,我没有使用任何库;