# PHP package for managing URI

The HttpSoft\Uri package implements Psr\Http\Message\UriInterface and normalizes the URI in accordance with the RFC 3986 specification. This functionality includes: parsing, validating, and modeling the URI, as well as later convenient manipulation of the entire URI and its parts separately.

This package requires PHP version 7.4 or later.

Package installation:

composer require httpsoft/http-uri

Source code on GitHub

# API

# Usage

use HttpSoft\Uri\Uri;
use HttpSoft\Uri\UriFactory;

$uri = new Uri('https://example.com/path/to/action?key1=value1&key2=val<>ue2');
// or
$uri = UriFactory::create('https://example.com/path/to/action?key1=value1&key2=val<>ue2');
// or
$factory = new UriFactory();
$uri = $factory->createUri('https://example.com/path/to/action?key1=value1&key2=val<>ue2');

$uri->getScheme(); // 'https'
$uri->getHost(); // 'example.com'

$uri->getPath(); // '/path/to/action'
$uriWithNewPath = $uri->withPath('new/path');
$uriWithNewPath->getPath(); // 'new/path'
$uri->getPath(); // '/path/to/action'

$uri->getQuery(); // 'key1=value1&key2=val%3C%3Eue2'
$uri->getRawQuery(); // 'key1=value1&key2=val<>ue2'

$uri->get(); // 'https://example.com/path/to/action?key1=value1&key2=val%3C%3Eue2'
// equivalently to:
(string) $uri; // echo $uri;
// unencoded:
$uri->raw(); // 'https://example.com/path/to/action?key1=value1&key2=val<>ue2'

Detailed description of using HttpSoft\Uri\Uri see here.

HttpSoft\Uri\UriNormalizer can be used independently:

use HttpSoft\Uri\UriNormalizer;

$normalizer = new UriNormalizer();

$uriData = $normalizer->normalize('https://example.com/pa<>th');
$uriData->getScheme(); // 'https'
$uriData->getHost(); // 'example.com'
$uriData->getPath(); // '/pa%3C%3Eth'
(string) $uriData; // 'https://example.com/pa%3C%3Eth'

// parse on the components but does not normalize the URI:
$uriRawData = $normalizer->normalize('https://example.com/pa<>th', true);
$uriData->getScheme(); // 'https'
$uriData->getHost(); // 'example.com'
$uriData->getPath(); // '/pa<>th'
(string) $uriData; // 'https://example.com/pa<>th'

Detailed description of using HttpSoft\Uri\UriNormalizer see here.