Class Uri
- GuzzleHttp\Psr7\Uri implements Psr\Http\Message\UriInterface
- Namespace: GuzzleHttp\Psr7
- Author: Michael Dowling
- Author: Tobias Schultze
- Author: Matthew Weier O'Phinney
- Located at Uri.php
PSR-7 URI implementation.
Methods Summary
- __construct ( string $uri = '' )
-
__toString ( )
Return the string representation as a URI reference.
-
static
composeComponents ( string $scheme, string $authority, string $path, string $query, string $fragment )
Composes a URI reference string from its various components.
-
static
isDefaultPort ( Psr\Http\Message\UriInterface $uri )
Whether the URI has the default port of the current scheme.
-
static
isAbsolute ( Psr\Http\Message\UriInterface $uri )
Whether the URI is absolute, i.e. it has a scheme.
-
static
isNetworkPathReference ( Psr\Http\Message\UriInterface $uri )
Whether the URI is a network-path reference.
-
static
isAbsolutePathReference ( Psr\Http\Message\UriInterface $uri )
Whether the URI is a absolute-path reference.
-
static
isRelativePathReference ( Psr\Http\Message\UriInterface $uri )
Whether the URI is a relative-path reference.
-
static
isSameDocumentReference ( Psr\Http\Message\UriInterface $uri, Psr\Http\Message\UriInterface $base = null )
Whether the URI is a same-document reference.
-
static
removeDotSegments ( string $path )
Removes dot segments from a path and returns the new path.
-
static
resolve ( Psr\Http\Message\UriInterface $base, string|UriInterface $rel )
Converts the relative URI into a new URI that is resolved against the base URI.
-
static
withoutQueryValue ( Psr\Http\Message\UriInterface $uri, string $key )
Creates a new URI with a specific query string value removed.
-
static
withQueryValue ( Psr\Http\Message\UriInterface $uri, string $key, string|null $value )
Creates a new URI with a specific query string value.
-
static
withQueryValues ( Psr\Http\Message\UriInterface $uri, array $keyValueArray )
Creates a new URI with multiple specific query string values.
-
static
fromParts ( array $parts )
Creates a URI from a hash of
parse_url
components. -
getScheme ( )
Retrieve the scheme component of the URI.
-
getAuthority ( )
Retrieve the authority component of the URI.
-
getUserInfo ( )
Retrieve the user information component of the URI.
-
getHost ( )
Retrieve the host component of the URI.
-
getPort ( )
Retrieve the port component of the URI.
-
getPath ( )
Retrieve the path component of the URI.
-
getQuery ( )
Retrieve the query string of the URI.
-
getFragment ( )
Retrieve the fragment component of the URI.
-
withScheme ( string $scheme )
Return an instance with the specified scheme.
-
withUserInfo ( string $user, null|string $password = null )
Return an instance with the specified user information.
-
withHost ( string $host )
Return an instance with the specified host.
-
withPort ( null|int $port )
Return an instance with the specified port.
-
withPath ( string $path )
Return an instance with the specified path.
-
withQuery ( string $query )
Return an instance with the specified query string.
-
withFragment ( string $fragment )
Return an instance with the specified URI fragment.
Methods Details
__construct ( string $uri = '' )
Parameters
string | $uri = '' | URI to parse |
string __toString ( )
Return the string representation as a URI reference.
Depending on which components of the URI are present, the resulting string is either a full URI or relative reference according to RFC 3986, Section 4.1. The method concatenates the various components of the URI, using the appropriate delimiters:
- If a scheme is present, it MUST be suffixed by ":".
- If an authority is present, it MUST be prefixed by "//".
- The path can be concatenated without delimiters. But there are two cases where the path has to be adjusted to make the URI reference valid as PHP does not allow to throw an exception in __toString(): - If the path is rootless and an authority is present, the path MUST be prefixed by "/". - If the path is starting with more than one "/" and no authority is present, the starting slashes MUST be reduced to one.
- If a query is present, it MUST be prefixed by "?".
- If a fragment is present, it MUST be prefixed by "#".
Returns
string |
See
Implementation of
static string composeComponents ( string $scheme, string $authority, string $path, string $query, string $fragment )
Composes a URI reference string from its various components.
Usually this method does not need to be called manually but instead is used indirectly via
Psr\Http\Message\UriInterface::__toString
.
PSR-7 UriInterface treats an empty component the same as a missing component as getQuery(), getFragment() etc. always return a string. This explains the slight difference to RFC 3986 Section 5.3.
Another adjustment is that the authority separator is added even when the authority is missing/empty
for the "file" scheme. This is because PHP stream functions like file_get_contents
only work with
file:///myfile
but not with file:/myfile
although they are equivalent according to RFC 3986. But
file:///
is the more common syntax for the file scheme anyway (Chrome for example redirects to
that format).
Parameters
string | $scheme | |
string | $authority | |
string | $path | |
string | $query | |
string | $fragment |
Returns
string |
Link
static
boolean
isDefaultPort (
Psr\Http\Message\UriInterface
$uri
)
Whether the URI has the default port of the current scheme.
Psr\Http\Message\UriInterface::getPort
may return null or the standard port. This method can be used
independently of the implementation.
Parameters
Psr\Http\Message\UriInterface |
$uri |
Returns
boolean |
static
boolean
isAbsolute (
Psr\Http\Message\UriInterface
$uri
)
Whether the URI is absolute, i.e. it has a scheme.
An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative to another URI, the base URI. Relative references can be divided into several forms: - network-path references, e.g. '//example.com/path' - absolute-path references, e.g. '/path' - relative-path references, e.g. 'subpath'
Parameters
Psr\Http\Message\UriInterface |
$uri |
Returns
boolean |
See
Uri::isAbsolutePathReference
Uri::isRelativePathReference
Link
static
boolean
isNetworkPathReference (
Psr\Http\Message\UriInterface
$uri
)
Whether the URI is a network-path reference.
A relative reference that begins with two slash characters is termed an network-path reference.
Parameters
Psr\Http\Message\UriInterface |
$uri |
Returns
boolean |
Link
static
boolean
isAbsolutePathReference (
Psr\Http\Message\UriInterface
$uri
)
Whether the URI is a absolute-path reference.
A relative reference that begins with a single slash character is termed an absolute-path reference.
Parameters
Psr\Http\Message\UriInterface |
$uri |
Returns
boolean |
Link
static
boolean
isRelativePathReference (
Psr\Http\Message\UriInterface
$uri
)
Whether the URI is a relative-path reference.
A relative reference that does not begin with a slash character is termed a relative-path reference.
Parameters
Psr\Http\Message\UriInterface |
$uri |
Returns
boolean |
Link
static
boolean
isSameDocumentReference (
Psr\Http\Message\UriInterface
$uri,
Psr\Http\Message\UriInterface
$base = null
)
Whether the URI is a same-document reference.
A same-document reference refers to a URI that is, aside from its fragment component, identical to the base URI. When no base URI is given, only an empty URI reference (apart from its fragment) is considered a same-document reference.
Parameters
Psr\Http\Message\UriInterface |
$uri | The URI to check |
Psr\Http\Message\UriInterface |
$base = null | An optional base URI to compare against |
Returns
boolean |
Link
static string removeDotSegments ( string $path )
Removes dot segments from a path and returns the new path.
Deprecated
Parameters
string | $path |
Returns
string |
See
static
Psr\Http\Message\UriInterface
resolve (
Psr\Http\Message\UriInterface
$base,
string|Psr\Http\Message\UriInterface
$rel
)
Converts the relative URI into a new URI that is resolved against the base URI.
Deprecated
Parameters
Psr\Http\Message\UriInterface |
$base | Base URI |
string|Psr\Http\Message\UriInterface |
$rel | Relative URI |
Returns
Psr\Http\Message\UriInterface |
See
static
Psr\Http\Message\UriInterface
withoutQueryValue (
Psr\Http\Message\UriInterface
$uri,
string
$key
)
Creates a new URI with a specific query string value removed.
Any existing query string values that exactly match the provided key are removed.
Parameters
Psr\Http\Message\UriInterface |
$uri | URI to use as a base. |
string | $key | Query string key to remove. |
Returns
Psr\Http\Message\UriInterface |
static
Psr\Http\Message\UriInterface
withQueryValue (
Psr\Http\Message\UriInterface
$uri,
string
$key,
string|null
$value
)
Creates a new URI with a specific query string value.
Any existing query string values that exactly match the provided key are removed and replaced with the given key value pair.
A value of null will set the query string key without a value, e.g. "key" instead of "key=value".
Parameters
Psr\Http\Message\UriInterface |
$uri | URI to use as a base. |
string | $key | Key to set. |
string|null | $value | Value to set |
Returns
Psr\Http\Message\UriInterface |
static
Psr\Http\Message\UriInterface
withQueryValues (
Psr\Http\Message\UriInterface
$uri,
array
$keyValueArray
)
Creates a new URI with multiple specific query string values.
It has the same behavior as withQueryValue() but for an associative array of key => value.
Parameters
Psr\Http\Message\UriInterface |
$uri | URI to use as a base. |
array | $keyValueArray | Associative array of key and values |
Returns
Psr\Http\Message\UriInterface |
static
Psr\Http\Message\UriInterface
fromParts (
array
$parts
)
Creates a URI from a hash of parse_url
components.
Parameters
array | $parts |
Returns
Psr\Http\Message\UriInterface |
Exceptions
InvalidArgumentException If the components do not form a valid URI. |
Link
string getScheme ( )
Retrieve the scheme component of the URI.
If no scheme is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.1.
The trailing ":" character is not part of the scheme and MUST NOT be added.
Returns
string The URI scheme. |
See
Implementation of
string getAuthority ( )
Retrieve the authority component of the URI.
If no authority information is present, this method MUST return an empty string.
The authority syntax of the URI is:
[user-info@]host[:port]
If the port component is not set or is the standard port for the current scheme, it SHOULD NOT be included.
Returns
string The URI authority, in "[user-info@]host[:port]" format. |
See
Implementation of
string getUserInfo ( )
Retrieve the user information component of the URI.
If no user information is present, this method MUST return an empty string.
If a user is present in the URI, this will return that value; additionally, if the password is also present, it will be appended to the user value, with a colon (":") separating the values.
The trailing "@" character is not part of the user information and MUST NOT be added.
Returns
string The URI user information, in "username[:password]" format. |
Implementation of
string getHost ( )
Retrieve the host component of the URI.
If no host is present, this method MUST return an empty string.
The value returned MUST be normalized to lowercase, per RFC 3986 Section 3.2.2.
Returns
string The URI host. |
See
Implementation of
null|integer getPort ( )
Retrieve the port component of the URI.
If a port is present, and it is non-standard for the current scheme, this method MUST return it as an integer. If the port is the standard port used with the current scheme, this method SHOULD return null.
If no port is present, and no scheme is present, this method MUST return a null value.
If no port is present, but a scheme is present, this method MAY return the standard port for that scheme, but SHOULD return null.
Returns
null|integer The URI port. |
Implementation of
string getPath ( )
Retrieve the path component of the URI.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
Normally, the empty path "" and absolute path "/" are considered equal as defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically do this normalization because in contexts with a trimmed base path, e.g. the front controller, this difference becomes significant. It's the task of the user to handle both "" and "/".
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.3.
As an example, if the value should include a slash ("/") not intended as delimiter between path segments, that value MUST be passed in encoded form (e.g., "%2F") to the instance.
Returns
string The URI path. |
See
https://tools.ietf.org/html/rfc3986#section-3.3
Implementation of
string getQuery ( )
Retrieve the query string of the URI.
If no query string is present, this method MUST return an empty string.
The leading "?" character is not part of the query and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.4.
As an example, if a value in a key/value pair of the query string should include an ampersand ("&") not intended as a delimiter between values, that value MUST be passed in encoded form (e.g., "%26") to the instance.
Returns
string The URI query string. |
See
https://tools.ietf.org/html/rfc3986#section-3.4
Implementation of
string getFragment ( )
Retrieve the fragment component of the URI.
If no fragment is present, this method MUST return an empty string.
The leading "#" character is not part of the fragment and MUST NOT be added.
The value returned MUST be percent-encoded, but MUST NOT double-encode any characters. To determine what characters to encode, please refer to RFC 3986, Sections 2 and 3.5.
Returns
string The URI fragment. |
See
https://tools.ietf.org/html/rfc3986#section-3.5
Implementation of
static withScheme ( string $scheme )
Return an instance with the specified scheme.
This method MUST retain the state of the current instance, and return an instance that contains the specified scheme.
Implementations MUST support the schemes "http" and "https" case insensitively, and MAY accommodate other schemes if required.
An empty scheme is equivalent to removing the scheme.
Parameters
string | $scheme | The scheme to use with the new instance. |
Returns
static A new instance with the specified scheme. |
Exceptions
InvalidArgumentException for invalid or unsupported schemes. |
Implementation of
static withUserInfo ( string $user, null|string $password = null )
Return an instance with the specified user information.
This method MUST retain the state of the current instance, and return an instance that contains the specified user information.
Password is optional, but the user information MUST include the user; an empty string for the user is equivalent to removing user information.
Parameters
string | $user | The user name to use for authority. |
null|string | $password = null | The password associated with $user. |
Returns
static A new instance with the specified user information. |
Implementation of
static withHost ( string $host )
Return an instance with the specified host.
This method MUST retain the state of the current instance, and return an instance that contains the specified host.
An empty host value is equivalent to removing the host.
Parameters
string | $host | The hostname to use with the new instance. |
Returns
static A new instance with the specified host. |
Exceptions
InvalidArgumentException for invalid hostnames. |
Implementation of
static withPort ( null|integer $port )
Return an instance with the specified port.
This method MUST retain the state of the current instance, and return an instance that contains the specified port.
Implementations MUST raise an exception for ports outside the established TCP and UDP port ranges.
A null value provided for the port is equivalent to removing the port information.
Parameters
null|integer | $port | The port to use with the new instance; a null value removes the port information. |
Returns
static A new instance with the specified port. |
Exceptions
InvalidArgumentException for invalid ports. |
Implementation of
static withPath ( string $path )
Return an instance with the specified path.
This method MUST retain the state of the current instance, and return an instance that contains the specified path.
The path can either be empty or absolute (starting with a slash) or rootless (not starting with a slash). Implementations MUST support all three syntaxes.
If the path is intended to be domain-relative rather than path relative then it must begin with a slash ("/"). Paths not starting with a slash ("/") are assumed to be relative to some base path known to the application or consumer.
Users can provide both encoded and decoded path characters. Implementations ensure the correct encoding as outlined in getPath().
Parameters
string | $path | The path to use with the new instance. |
Returns
static A new instance with the specified path. |
Exceptions
InvalidArgumentException for invalid paths. |
Implementation of
static withQuery ( string $query )
Return an instance with the specified query string.
This method MUST retain the state of the current instance, and return an instance that contains the specified query string.
Users can provide both encoded and decoded query characters. Implementations ensure the correct encoding as outlined in getQuery().
An empty query string value is equivalent to removing the query string.
Parameters
string | $query | The query string to use with the new instance. |
Returns
static A new instance with the specified query string. |
Exceptions
InvalidArgumentException for invalid query strings. |
Implementation of
static withFragment ( string $fragment )
Return an instance with the specified URI fragment.
This method MUST retain the state of the current instance, and return an instance that contains the specified URI fragment.
Users can provide both encoded and decoded fragment characters. Implementations ensure the correct encoding as outlined in getFragment().
An empty fragment value is equivalent to removing the fragment.
Parameters
string | $fragment | The fragment to use with the new instance. |
Returns
static A new instance with the specified fragment. |
Implementation of
Constants summary
string |
HTTP_DEFAULT_HOST
Absolute http and https URIs require a host per RFC 7230 Section 2.7 but in generic URIs the host can be empty. So for http(s) URIs we apply this default host when no host is given yet to form a valid URI. |
#
'localhost'
|