PHP code example of phrity / net-uri

1. Go to this page and download the library: Download phrity/net-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/ */

    

phrity / net-uri example snippets


use Phrity\Net\Uri;
$uri = new Uri('http://example.com/path/to/file.html?query1=1#fragment');

// PSR-7 getters
$uri->getScheme();
$uri->getHost();
$uri->getPort();
$uri->getPath();
$uri->getQuery();
$uri->getFragment();
$uri->getAuthority();
$uri->getUserInfo();

// PSR-7 setters
$uri->withScheme('https');
$uri->withHost('example2.com');
$uri->withPort(8080);
$uri->withPath('/path/to/another/file.html');
$uri->withQuery('query2=2');
$uri->withFragment('another-fragment');
$uri->withUserInfo('username', 'password');

// Additional methods
$uri->toString();
$uri->__toString();
$uri->jsonSerialize();
$uri->getQueryItems();
$uri->getQueryItem('query1');
$uri->withQueryItems(['query1' => '1', 'query2' => '2']);
$uri->withQueryItem('query1', '1');
$uri->getComponents();
$uri->withComponents(['scheme' => 'https', 'host' => 'example2.com']);

use Phrity\Net\UriFactory;
$factory = new UriFactory();
$factory->createUri('http://example.com/path/to/file.html');
$factory->createUriFromInterface(new GuzzleHttp\Psr7\Uri('http://example.com/path/to/file.html'));

$uri = new Uri('http://example.com');
$uri->getPort(Uri::REQUIRE_PORT); // => 80
$uri->toString(Uri::REQUIRE_PORT); // => 'http://example.com:80'

$uri = new Uri('a/./path/../to//something');
$uri->getPath(Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH); // => '/a/to/something'
$uri->toString(Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH); // => '/a/to/something'

$clone = $uri->withPath('path/./somewhere/else/..', Uri::ABSOLUTE_PATH | Uri::NORMALIZE_PATH);
$clone->getPath(); // => '/path/somewhere'

$uri = new Uri('https://ηßöø必Дあ.com');
$uri->getHost(Uri::IDN_ENCODE); // => 'xn--zca0cg32z7rau82strvd.com'