Core\Uri::resolved
$uri->resolved(string $reference): Core\Uri$uri->resolved( string $reference, ): Core\Uri
Turns a link into a full address, the same way a browser does when you click a link on a page. The address of the page is the base, and you call resolved on it. For the base https://example.com/blog/post and the link ../about, the result is https://example.com/about.
A link can be a full address, a path such as /about or photo.jpg, a query such as ?page=2, or a fragment such as #top. The result is always a full address with a scheme. The . and .. parts are removed from its path. The base itself does not change, and its own fragment is not used.
The base must have a scheme, such as https:. A base such as /blog/post throws a RuntimeError. A link with a character that is not allowed in an address, such as a space, also throws one.
The examples below follow the links on a page, remove . and .. from a path, and collect the links of a page for a crawler.
Parameters
$referencestringThe link to follow. It can be a full address, a path such as/aboutorphoto.jpg, a query such as?page=2, or a fragment such as#top.
Return value
Returns Core\Uri. A new Uri with a scheme. The fragment of the base is not used. The base itself does not change.
Errors
RuntimeErrorThe base has no scheme (the part before:, such ashttps), as in/blog/post. The link has a character that is not allowed in an address, such as a space. Or the base has no/after its scheme, such asmailto:ann@example.com, so there is no path to add the link to.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Uri->resolved() and signed() return a new address and leave the original unchanged. The old names were resolve and sign. |
Examples
Follow links on a page
<?nvs// Shows how the links on a page become full addresses, the way a browser follows them.
Core\Uri $page = Core\Uri::parse('https://example.com/blog/2024/post.html?lang=en#comments');
array<string> $links = [ 'photo.jpg', '/about', '../archive', '?lang=de', '#top', 'https://example.org/news',];
// Each link is read from the address of the page.// The fragment `#comments` of the page is not used.foreach ($links as string $link) { echo $link, ' -> ', $page->resolved($link)->toString(), "\n";}photo.jpg -> https://example.com/blog/2024/photo.jpg/about -> https://example.com/about../archive -> https://example.com/blog/archive?lang=de -> https://example.com/blog/2024/post.html?lang=de#top -> https://example.com/blog/2024/post.html?lang=en#tophttps://example.org/news -> https://example.org/newsRemove dots from a path
<?nvs// Shows how `resolved` removes the `.` and `..` parts of a path.
Core\Uri $docs = Core\Uri::parse('https://example.com/docs/guide/');
// `.` is the current folder and `..` is the folder above it.// This prints https://example.com/docs/api/start.echo $docs->resolved('../api/./start')->toString(), "\n";
// A `..` at the top of the path is removed, so the result stays on the site.// This prints https://example.com/secret.echo $docs->resolved('../../../../secret')->toString(), "\n";
// An empty link returns the address of the base itself.// This prints https://example.com/docs/guide/.echo $docs->resolved('')->toString(), "\n";https://example.com/docs/api/starthttps://example.com/secrethttps://example.com/docs/guide/Collect links for a crawler
<?nvs// Shows how a crawler turns the links of a page into full addresses and keeps the ones on the same site.
class Crawler{ public static function sameSite(string $pageAddress, array<string> $links): array<string> { Core\Uri $page = Core\Uri::parse($pageAddress); array<string> $found = []; foreach ($links as string $link) { try { Core\Uri $target = $page->resolved($link); if ($target->host() == $page->host()) { $found[] = $target->toString(); } } catch (RuntimeError $error) { // A link with a space is not a valid address, so it is skipped. echo 'skipped: ', $link, "\n"; } } return $found; }}
array<string> $links = ['/shop/shoes', 'sale.html', 'https://ads.example.net/', 'size guide.pdf'];
// This prints one skipped link, then two addresses on shop.example.com.foreach (Crawler::sameSite('https://shop.example.com/shop/', $links) as string $address) { echo $address, "\n";}skipped: size guide.pdfhttps://shop.example.com/shop/shoeshttps://shop.example.com/shop/sale.html