PHP code example of fm-labs / php-uri

1. Go to this page and download the library: Download fm-labs/php-uri library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fm-labs / php-uri example snippets


// > Create new Uri
$uri = \FmLabs\Uri\UriFactory::create();
$uri = $uri
    ->withScheme('https')
    ->withHost('example.org')
    ->withPort(8080)
    ->withPath('/my/path')
    ->withQuery('foo=bar&hello=world')
    ->withFragment('top')
    ->withUserInfo('user', 's3cret');

echo (string)$uri;
// https://user:[email protected]:8080/my/path?foo=bar&hello=world#top

// > Create Uri from string
$uri = \FmLabs\Uri\UriFactory::fromString('http://user:[email protected]/test?q=hello#world');

// PSR-7 interface methods
$schema = $uri->getScheme(); // "http"
$host = $uri->getHost(); // "www.example.org"
$path = $uri->getPath(); // "/test"
$frag = $uri->getFragment(); // "world"
$userinfo = $uri->getUserInfo(); // "user:s3cret"
$authority = $uri->getAuthority(); // "user:[email protected]"

// Convenience methods
$user = $uri->getUser(); // "user"
$pass = $uri->getUserPass(); // "s3cret"
$queryData = $uri->getQueryData(); // ['q' => 'hello']
$queryData = $uri->getQueryData('q'); // 'hello'

// Array access (read-only)
$host = $uri['host'];

// Property access (read-only)
$host = $uri->host;

/** @var \FmLabs\Uri\Uri $uri **/

// Array access
$uri['KEY_NAME'];

// Property access
$uri->KEY_NAME;

// Examples
$uri = \FmLabs\Uri\UriFactory::create();
$uri = $uri
    ->withScheme('https')
    ->withHost('example.org');

// Examples
\FmLabs\Uri\UriFactory::fromString('http://www.example.org');
\FmLabs\Uri\UriFactory::fromString('https://user:[email protected]/my/path?some=query#frag');
\FmLabs\Uri\UriFactory::fromString('https://[email protected]:123/forum/questions/?tag=networking&order=newest#top');
\FmLabs\Uri\UriFactory::fromString('mailto:[email protected]');
\FmLabs\Uri\UriFactory::fromString('tel:+1-816-555-1212');
\FmLabs\Uri\UriFactory::fromString('ldap://[2001:db8::7]/c=GB?objectClass?one');
\FmLabs\Uri\UriFactory::fromString('urn:oasis:names:specification:docbook:dtd:xml:4.1.2');

// Examples
// http://www.example.org
\FmLabs\Uri\UriFactory::fromComponents(['scheme' => 'http', 'host' => 'www.example.org']);
// tel:+123456789
\FmLabs\Uri\UriFactory::fromComponents(['scheme' => 'tel', 'path' => '+123456789']);

/** @var \Psr\Http\Message\UriInterface $anyObjectThatImplementsUriInterface */
$uri = \FmLabs\Uri\UriFactory::fromUri($anyObjectThatImplementsUriInterface);

$uri = \FmLabs\Uri\UriFactory::fromString('hTTp://www.eXample.org:80/test/./../foo/../bar');
$normalized = \FmLabs\Uri\UriNormalizer::normalize($uri);
// http://www.example.org/test/foo/bar;

$uri = \FmLabs\Uri\UriFactory::fromString('hTTp://www.eXample.org:80/test/./../foo/../bar');
$normalizer = new \FmLabs\Uri\UriNormalizer($uri);
$uri = $normalizer
    // these normalizations preserve semantics of uri
    ->normalizeScheme()
    ->normalizeHost()
    ->normalizeDotSegements()
    ->normalizeTrailingSlash()
    ->normalizeUnreservedChars()
    ->normalizeEscapeSequences()
    ->normalizeDefaultPorts()
    // these normalizations change semantics of uri
    ->normalizeForceHttps()
    ->normalizeHostIp()
    ->normalizeWwwDomain()
    ->normalizeNonEmptyPath()
    ->normalizeDirectoryIndex()
    ->normalizeDuplicateSlashes()
    ->normalizeFragment()
    ->normalizeQuerySorting()
    ->normalizeEmptyQuery()
    ->getUri();

$normalizedUri = \FmLabs\Uri\UriNormalizer::normalize($uri, ['preserve' => false]);
// http://www.example.org/test/foo/bar;