URIs
Learn how we represent URIs
This library is abandoned. Please consider using a different library.
URIs are represented with the help of the Opis\Http\Uri
class.
The role of this class is to decompose an URI into its basic components and provide developerds various methods that
allow them to read the value of these individual components.
Constructing an URI is supper-easy and can be accomplished just as simple as bellow:
use Opis\Http\Uri;
$uri = new Uri("http://example.com");
echo $uri->getHost(); //> example.com
An URI object can be converted back to string through a simple type casting.
use Opis\Http\Uri;
$uri = new Uri('http://example.com');
// explicit type casting
$value = (string) $uri;
echo $value; //> http://example.com
// implicit type catsing
echo $uri; //> http://example.com
Components
You can obtain an array of components by calling the getComponents
method. The method will return
a key-value mapped array where the key represents the component’s name and the value represents the component’s value.
$components = $uri->getComponents();
The following components are available for the following URI http://user:pass@example.com:8080/foo?bar=baz#qux
:
scheme
- http.authority
- user:pass@example.com:8080user_info
- user:passhost
- example.com:8080port
- 8080. When the port is not explicitly specified, the value of this components is derived from the scheme component: 80 when the scheme is http, 443 when the scheme is https, andnull
otherwise.path
- /fooquery
- bar=bazfragment
- qux
The components of an URI can be accessed individually by using the following methods:
getScheme
- read thescheme
componentgetAuthority
- read theauthority
componentgetUserInfo
- read theuser_info
componentgetHost
- read thehost
componentgetPort
- read theport
componentgetPath
- read thepath
componentgetQuery
- read thequery
componentgetFragment
- read thefragment
component
You can also construct an URI by passing an array of components to its constructor.
use Opis\Http\Uri;
$components = [
'scheme' => 'https',
'host' => 'example.com',
'path' => '/test'
];
$uri = new Uri($components);
echo $uri->getPort(); //> 443
echo $uri->getPath(); //> /test
echo $uri; //>https://example.com/test