Skip to content

14: Identify URI and Host Types

Classify URL components for a report. The URI extension exposes distinct methods for the URI form, host form, and whether a WHATWG scheme is special.

  • 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-types.php example.

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

Terminal window
php code/php-8-6/14-uri-types/uri-types.php
uri-types.php
<?php
declare(strict_types=1);
use Uri\Rfc3986\Uri;
use Uri\WhatWg\Url;
$uri = new Uri('//assets.example/logo.svg');
echo 'URI type: ', $uri->getUriType()->name, PHP_EOL;
echo 'RFC host type: ', $uri->getHostType()->name, PHP_EOL;
$url = new Url('https://192.0.2.4/logo.svg');
echo 'WHATWG host type: ', $url->getHostType()->name, PHP_EOL;
echo 'Special scheme: ', $url->isSpecialScheme() ? 'yes' : 'no', PHP_EOL;
URI type: NetworkPathReference
RFC host type: RegisteredName
WHATWG host type: IpV4
Special scheme: yes

RFC 3986 separates absolute URIs from several kinds of references. WHATWG special schemes, such as HTTPS, use rules that differ from opaque schemes. A nullable host type lets you account for input without a host.

Inspect https://[2001:db8::1]/ and docs/readme. Confirm that the first has an IPv6 host type and the second has a relative path reference with no host.

Continue with Percent-Encode WHATWG URL Components.