Skip to content

13: Build URLs and URI References from Components

Assemble an absolute HTTPS URL and resolve a relative URI reference against a base. The builders give each component its own setter.

  • PHP 8.6 CLI. Confirm the version with php --version.
  • The uri extension.
  • Review the previous chapter if you’re following the series in order.

Allow about 10 minutes for the walkthrough and exercise.

A runnable uri-builders.php example.

Run the following from the root of the Code with PHP repository.

Terminal window
php code/php-8-6/13-uri-builders/uri-builders.php
uri-builders.php
<?php
declare(strict_types=1);
use Uri\Rfc3986\Uri;
use Uri\Rfc3986\UriBuilder;
use Uri\WhatWg\UrlBuilder;
$query = http_build_query(['city' => 'New Town'], encoding_type: PHP_QUERY_RFC3986);
$url = new UrlBuilder()
->setScheme('https')
->setHost('Travel.Example')
->setPort(443)
->setPath('/search')
->setQuery($query)
->build();
$reference = new UriBuilder()
->setPath('guides/tasmania')
->build(new Uri('https://travel.example/catalog/'));
echo $url->toAsciiString(), PHP_EOL;
echo $reference->toString(), PHP_EOL;
https://travel.example/search?city=New%20Town
https://travel.example/catalog/guides/tasmania

UriBuilder follows RFC 3986 rules for URIs and references. UrlBuilder follows the WHATWG rules used by browsers, including normalization such as dropping HTTPS’s default port. Choose the model that matches the consumer of the result.

Change the HTTPS port to 8443. Check that the URL includes :8443. Resolve images/map.png against the same RFC 3986 base and check the resulting path.

Continue with Identify URI and Host Types.