This library is a PSR-7 implementation used by Shieldon firewall 2 version, following up the PSR-7 HTTP message interfaces document. You can use it on any framework which is compatible with the PSR-7 standard.
composer require shieldon/psr7
The Shieldon PSR-7 implementation requires at least PHP 7.1 to run.
The usages of every method can be found in the unit tests.
Here are some examples that show you the way creating PSR-7 instances from PSR-17 HTTP factories.
None
$method, $uri)string method * The HTTP method associated with the request.
UriInterface|string uri * The URI associated with the request.
RequestInterface
Example:
use Shieldon\Psr15\RequestFactory;
$requestFactory = new RequestFactory();
$request = $requestFactory->createRequest('GET', 'https://www.google.com');
$autoDetermine)bool autoDetermine = false Determine HTTP method and URI automatically.
Example:
use Shieldon\Psr15\ServerRequestFactory;
$serverRequestFactory = new ServerRequestFactory(true);
PSR-17 document says, In particular, no attempt is made to determine the HTTP method or URI, which must be provided explicitly.
I think that HTTP method and URI can be given by superglobal in SAPI enviornment, since it is a server-side request. This is an option to allow you automatically determine the HTTP method and URI when $method and $uri are empty.
$method, $uri, $serverParams)string method *The HTTP method associated with the request.
UriInterface|string uri *The URI associated with the request.
array serverParams = [] An array of Server API (SAPI) parameters with which to seed the generated request instance.
ServerRequestInterface
Examples:
Determine HTTP method and URI automatically.
$serverRequestFactory = new ServerRequestFactory(true);
$serverRequest = $serverRequestFactory->createServerRequest('', '');
Or, the HTTP method and URI must be provided explicitly.
$serverRequestFactory = new ServerRequestFactory();
$method = 'GET';
$url = 'https://www.yourwebsite.com/current-page/';
$serverRequest = $serverRequestFactory->createServerRequest($method, $uri);
None
$code, $reasonPhrase)int code = 200 The HTTP status code.
string reasonPhrase = '' The reason phrase to associate with the status code.
ResponseInterface
Example:
use Shieldon\Psr15\ResponseFactory;
$responseFactory = new ResponseFactory();
$response = $responseFactory->createResponse(200, 'OK');
None
$content)string content = "" String content with which to populate the stream.
StreamInterface
Example:
$streamFactory = new StreamFactory();
$stream = $streamFactory->createStream('Foo Bar');
echo $stream;
// Outputs: Foo Bar
$filename, $mode)string filename * The filename or stream URI to use as basis of stream.
string mode r The mode with which to open the underlying filename/stream.
StreamInterface
Example:
$sourceFile = BOOTSTRAP_DIR . '/sample/shieldon_logo.png';
$streamFactory = new StreamFactory();
$stream = $streamFactory->createStreamFromFile($sourceFile);
echo $stream->getSize();
// Outputs: 15166
$resource)string resource * The PHP resource to use as the basis for the stream.
StreamInterface
Example:
$streamFactory = new StreamFactory();
$stream = $streamFactory->createStreamResource(
fopen('php://temp', 'r+')
);
None
$stream, $size, $error, $clientFilename, $clientMediaType)StreamInterface stream * The underlying stream representing the uploaded file content.
int|null size = null The size of the file in bytes.
int error = 0 The PHP file upload error.
string|null clientFilename = null The filename as provided by the client, if any.
string|null clientMediaType = null The media type as provided by the client, if any.
UploadedFileInterface
Example:
$uploadedFileFactory = new UploadedFileFactory();
$sourcePath = BOOTSTRAP_DIR . '/sample/shieldon_logo.png';
$targetPath = STORAGE_DIR . '/images/shieldon_logo.png';
$streamFactory = new StreamFactory();
$uploadedFileFactory = new UploadedFileFactory();
$stream = $streamFactory->createStreamFromFile($sourcePath);
$uploadedFile = $uploadedFileFactory->createUploadedFile($stream);
// Move file from $sourcePath to $targetPath.
$uploadedFile->moveTo($targetPath);
None
$uri)string uri = "" The URI to parse.
UriInterface
Example:
$uriFactory = new UriFactory;
$uri = $uriFactory->createUri();
Note:
Here only shows the PSR-7 methods because other non-PSR methods are just helpers. They are listed on the bottom of this page, you can check out them if you are interested.
None
Example:
$message = new \Shieldon\Psr7\Message();
string HTTP protocol version.
Example:
echo $message->getProtocolVersion();
// Outputs: 1.1
$version)string version * HTTP protocol version.
static
array (string[][]) Each key is a header name, and each value is an array of strings for that header
Example:
$headers = $message->getHeaders();
print(print_r($headers, true));
/* Outputs:
Array
(
[user-agent] => Array
(
[0] => Mozilla/5.0 (Windows NT 10.0; Win64; x64)
)
[host] => Array
(
[0] => 127.0.0.1
)
[accept] => Array
(
[0] => text/html,application/xhtml+xml,application/xml;q=0.9
)
[accept_charset] => Array
(
[0] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
)
[accept_language] => Array
(
[0] => en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7
)
)
*/
$name)string name * Case-insensitive header field name.
bool
Example:
if ($message->hasHeader('user-agent')) {
echo 'Header user-agent exists.';
} else {
echo 'Header user-agent does not exist.';
}
// Outputs: Header user-agent exists.
$name)string name * Case-insensitive header field name.
array An array of string values as provided for the given. Return empty array if the header dosn't exist.
Example:
$useragent = $this->message->getHeader('user-agent');
print(print_r($useragent, true));
/* Outputs:
Array
(
[0] => Mozilla/5.0 (Windows NT 10.0; Win64; x64)
)
*/
$useragent = $this->message->getHeader('does-not-exist');
print(print_r($useragent, true));
/* Outputs:
Array()
*/
$name)string name * Case-insensitive header field name.
array a string values as provided for the given header concatenated together using a comma. Return empty string if the header dosn't exist.
Example:
echo $this->message->getHeaderLine('user-agent');
// Outputs: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
$name, $value)string name * Case-insensitive header field name.
string|array value * Header value(s)
static
Example:
$message = $message->withHeader('foo', 'bar');
echo $message->getHeaderLine('foo');
// Outputs: bar
echo $message->getHeaderLine('FOO');
// Outputs: bar
$message = $message->withHeader('fOO', 'baz');
echo $message->getHeaderLine('foo');
// Outputs: baz
$message = $message->withHeader('fOO', ['bax', 'bay', 'baz']);
echo $message->getHeaderLine('foo');
// Outputs: bax, bay, baz
$name, $value)string name * Case-insensitive header field name.
string|array value * Header value(s)
static
Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added.
Example:
$message = $message->withHeader('foo', 'bar');
echo $message->getHeaderLine('foo');
// Outputs: bar
$message = $message->withAddedHeader('foo', 'baz');
echo $message->getHeaderLine('foo');
// Outputs: bar
$message = $message->withAddedHeader('foo2', 'baz');
echo $message->getHeaderLine('foo2');
// Outputs: baz
$name)string name * Case-insensitive header field name.
static
Example:
$message = $message->withHeader('foo', 'bar');
echo $message->getHeaderLine('foo');
// Outputs: bar
$message = $message->withoutHeader('foo');
echo $message->getHeaderLine('foo');
// Outputs:
StreamInterface
Example:
$stream = $message->getBody();
// Assume the content is a HTML formatted string.
// getContent() is a method defined in StreamInterface.
echo $stream->getContents();
// Outputs: <html>...</html>
$body)StreamInterface body * Body.
static
Example:
$stream = new \Shieldon\Psr7\Stream(fopen('php://temp', 'r+'));
$stream->write('Foo Bar');
$message = $message->withBody($stream);
echo $message->getBody()->getContents();
// Outputs: Foo Bar
$method, $uri, $body, $headers, $version)string method = "GET" Request HTTP method.
string|UriInterface uri = "" Request URI object URI or URL.
string|StreamInterface body = "" Request body - see setBody()
array headers = [] Request headers.
string version = "1.1" Request protocol version.
Example:
$request = new \Shieldon\Psr7\Request('GET', 'https://www.example.com');
string
In most cases, this will be the origin-form of the composed URI, unless it is changed by withRequestTarget method.
Example:
echo $request->getRequestTarget();
// Outputs: /
$requestTarget)string requestTarget *
static
Example:
$request = $request->withRequestTarget('https://www.example2.com/en/');
echo $request->getRequestTarget();
// Outputs: https://www.example2.com/en/
string
Example:
echo $request->getMethod();
// Outputs: GET
$method)string method * Case-sensitive method.
static
Example:
$request = $request->withMethod('POST');
echo $request->getMethod();
// Outputs: POST
UriInterface
Example:
echo $request->getUri()->getHost();
// Outputs: www.example.com
$uri, $preserveHost)UriInterface uri * New request URI to use.
string preserveHost * Preserve the original state of the Host header.
static
Example:
$request = new Request('GET', 'https://terryl.in/zh/', '', [], '1.1');
$newRequest = $request->withUri(new Uri('https://play.google.com'));
$newRequest2 = $newRequest->withUri(new Uri('https://www.facebook.com'), true);
echo $newRequest->getUri()->getHost();
// Outputs: play.google.com
echo $newRequest2->getUri()->getHost();
// Outputs: terryl.in
string method = "GET" Request HTTP method.
string|UriInterface uri = "" Request URI object URI or URL.
string|StreamInterface body = "" Request body.
array headers = [] Request headers.
string version = "1.1" Request protocol version.
array serverParams = [] Typically $_SERVER superglobal.
array cookieParams = [] Typically $_COOKIE superglobal.
array postParams = [] Typically $_POST superglobal.
array getParams = [] Typically $_GET superglobal.
array filesParams = [] Typically $_FILES superglobal.
Example:
$serverRequest = new \Shieldon\Psr7\ServerRequest();
array
Example:
$serverParams = $serverRequests->getServerParams();
print(print_r($serverParams, true));
/* Outputs:
Array
(
[USER] => vagrant
[HOME] => /home/vagrant
[HTTP_COOKIE] => PHPSESSID=pca6qln5ab1k7ihthqvuo7rtietguapm
[HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml
[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64)
[HTTP_UPGRADE_INSECURE_REQUESTS] => 1
[HTTP_CACHE_CONTROL] => max-age=0
[HTTP_CONNECTION] => keep-alive
[HTTP_HOST] => nantou.welcometw.lo
[CI_ENV] => development
[SCRIPT_FILENAME] => /home/terrylin/public/index.php
[REDIRECT_STATUS] => 200
[SERVER_NAME] => terryl.lo
[SERVER_PORT] => 80
[SERVER_ADDR] => 192.168.33.33
[REMOTE_PORT] => 64557
[REMOTE_ADDR] => 192.168.33.1
[SERVER_SOFTWARE] => nginx/1.14.0
[GATEWAY_INTERFACE] => CGI/1.1
[REQUEST_SCHEME] => http
[SERVER_PROTOCOL] => HTTP/1.1
[DOCUMENT_ROOT] => /home/terrylin/public
[DOCUMENT_URI] => /index.php
[REQUEST_URI] => /
[SCRIPT_NAME] => /index.php
[CONTENT_LENGTH] =>
[CONTENT_TYPE] =>
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[FCGI_ROLE] => RESPONDER
[PHP_SELF] => /index.php
[REQUEST_TIME_FLOAT] => 1591868770.3356
[REQUEST_TIME] => 1591868770
)
*/
array
Example:
$cookieParams = $serverRequests->getCookieParams();
print(print_r($cookieParams, true));
/* Outputs:
Array
(
[foo] => bar
)
*/
array
Example:
// https://www.example.com/?foo=bar
$queryParams = $serverRequests->getQueryParams();
print(print_r($queryParams, true));
/* Outputs:
Array
(
[foo] => bar
)
*/
$query)array query * Array of query string arguments, typically from $_GET.
static
Example:
$serverRequests = $serverRequests->withQueryParams([
'foo' => 'baz',
'yes' => 'I do',
]);
$queryParams = $serverRequests->getQueryParams();
print(print_r($queryParams, true));
/* Outputs:
Array
(
[foo] => baz
[yes] => I do
)
*/
array
Example:
$_FILES['avatar'] = [
'tmp_name' => '/tmp/phpmFLrzD',
'name' => 'my-avatar.png',
'type' => 'image/png',
'error' => 0,
'size' => 90996,
];
$serverRequest = new \Shieldon\Psr7\ServerRequest(
'GET',
'',
'',
[],
'1.1',
[],
[],
[],
[],
$_FILES
);
echo $serverRequests->getUploadedFiles()->getClientFilename();
// Outputs: my-avatar.png
echo $serverRequests->getUploadedFiles()->getClientMediaType();
// Outputs: image/png
null|array|object
Example:
// Typically, $parsedBody is equal to $_POST superglobal.
$parsedBody = $serverRequest->getParsedBody();
$data)null|array|object $data * The deserialized body data.
static
Example:
$serverRequest = $serverRequest->withParsedBody(
[
'foo' => 'bar',
'yes' => 'I do'
]
);
$parsedBody = $serverRequest->getParsedBody();
echo $parsedBody['yes'];
// Outputs: I do
array
Example:
$_SESSION['user_name'] = 'terrylin';
$_SESSION['user_role'] = 'admin';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$serverRequest = $serverRequest->
withAttribute('session', $_SESSION)->
withAttribute('ip_address', $_SERVER['REMOTE_ADDR']);
$attributes = $serverRequest->getAttributes();
echo $attributes['session']['user_name'];
// Outputs: terrylin
echo $attributes['ip_address'];
// Outputs: 127.0.0.1
$name, $default)string name * The attribute name.
mixed filesParams = null Default value to return if the attribute does not exist.
mixed
Example:
This example extends to the previous one.
$ip = $serverRequest->getAttribute('ip_address');
$session = $serverRequest->getAttribute('session');
// paymentStatus does not exist.
$paymentStatus = $serverRequest->getAttribute('paymentStatus', 'failed');
echo $ip
// Outputs: 127.0.0.1
echo $session['user_role'];
// Outputs: admin
echo $paymentStatus;
// Outputs: failed
$name, $value)string name * The attribute name.
mixed value * The value of the attribute.
static
Example:
$serverRequest = $serverRequest->withAttribute('ip_address', '19.89.6.4');
$ip = $serverRequest->getAttribute('ip_address');
echo $ip
// Outputs: 19.89.6.4
$name)string name * The attribute name.
static
Example:
$serverRequest = $serverRequest->withoutAttribute('ip_address');
$ip = $serverRequest->getAttribute('ip_address', 'undefined');
echo $ip
// Outputs: undefined
int status = 200 Response HTTP status code.
array headers = [] Response headers.
StreamInterface|string body = "" Response body.
string version = "1.1" Response protocol version.
string reason = "OK" Reasponse HTTP reason phrase.
Example:
$response = new \Shieldon\Psr7\Response();
int
Example:
$statusCode = $response->getStatusCode();
echo $statusCode
// Outputs: 200
$code, $reasonPhrase)string code * The 3-digit integer result code to set.
string reasonPhrase = "" The reason phrase to use with the provided status code
static
Example:
$response = $response->withStatus(599, 'Something went wrong.');
echo $response->getStatusCode();
// Outputs: 599
echo $response->getReasonPhrase();
// Outputs: Something went wrong.
string
Example:
$reasonPhrase = $response->getReasonPhrase();
echo $reasonPhrase
// Outputs: OK
resource stream * A valid resource.
Example:
$stream = new \Shieldon\Psr7\Stream(fopen('php://temp', 'r+'));
Write data to the stream.
bool
Example:
$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new \Shieldon\Psr7\Stream($resource);
if ($stream->isWritable()) {
echo 'File is writable';
}
// Outputs: File is writable
Returns whether or not the stream is readable.
bool
Example:
$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new \Shieldon\Psr7\Stream($resource);
if ($stream->isReadable()) {
echo 'File is readable';
}
// Outputs: File is readable
Seek to a position in the stream.
bool
Example:
$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new \Shieldon\Psr7\Stream($resource);
if ($stream->isSeekable()) {
echo 'File is seekable';
}
// Outputs: File is seekable
loses the stream and any underlying resources.
void
Example:
$stream = new Stream(fopen('php://temp', 'r+'));
/* ... do something ... */
$stream->close();
Separates any underlying resources from the stream. After the stream has been detached, the stream is in an unusable state.
resource|null
Example:
$stream = new Stream(fopen('php://temp', 'r+'));
/* ... do something ... */
$legacy = $stream->detach();
if (is_resouce($legacy)) {
echo 'Resource is detached.';
}
// Outputs: Resource is detached.
$legacy = $stream->detach();
if (is_null($legacy)) {
echo 'Resource has been null.';
}
// Outputs: Resource has been null.
Get the size of the stream if known.
int|null
Example:
$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new \Shieldon\Psr7\Stream($resource);
echo $stream->getSize();
// Outputs: 15166
Returns the current position of the file read/write pointer
int Position of the file pointer$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new Stream($resource);
$stream->seek(10);
echo $stream->tell();
// Outputs: 10
$stream->rewind();
echo $stream->tell();
// Outputs: 0
$stream->close();
Returns true if the stream is at the end of the stream.
bool
$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new Stream($resource);
$stream->seek(10);
if ($stream->eof()) {
echo 'The position of the file pointer of the stream is at the end.';
} else {
echo 'Not at the end.';
}
// Outputs: Not at the end.
$stream->seek(15166);
if ($stream->eof()) {
echo 'The position of the file pointer of the stream is at the end.';
} else {
echo 'Not at the end.';
}
// Outputs: The position of the file pointer of the stream is at the end.
$offset, $whence)Seek to a position in the stream.
int offset * Stream offset.
int whence = SEEK_SET Specifies how the cursor position will be calculated based on the seek offset.
void
Example:
// See eof() example.
Seek to the beginning of the stream.
void
Example:
// See tell() example.
$string)string string * The string that is to be written.
int Returns the number of bytes written to the stream.
Example:
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write('Foo Bar');
echo $stream->getContents();
// Outputs: Foo Bar
$length)Read data from the stream.
int length * Read up to $length bytes from the object and return them.
string
Example:
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write('Glory to Hong Kong');
echo $stream->read(5);
// Outputs: Glory
Returns the remaining contents in a string
string
Example:
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write('Glory to Hong Kong');
echo $stream->getContents();
// Outputs: Glory to Hong Kong
$key)Get stream metadata as an associative array or retrieve a specific key.
string key = null Specific metadata to retrieve.
array|mixed|null
Example:
$resource = fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+');
$stream = new Stream($resource);
$meta = $stream->getMetadata();
print(print_r($queryParams, true));
/* Outputs:
Array
(
[timed_out] => false
[blocked] => true
[eof] => false
[wrapper_type] => plainfile
[stream_type] => STDIO
[mode] => r+
[unread_bytes] => 0
[seekable] => true
[uri] => /home/terrylin/data/psr7/tests/sample/shieldon_logo.png
)
*/
echo $stream->getMetadata('mode')
// Outputs: r+
Reads all data from the stream into a string, from the beginning to end.
string
Example:
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write('Foo Bar');
echo $stream;
// Outputs: Foo Bar
string|StreamInterface source * The full path of a file or stream.
string|null name = null The file name.
string|null type = null The file media type.
int|null size = null The file size in bytes.
int error = 0 The status code of the upload.
string|null sapi = null Only assign for unit testing purpose.
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp', // source
'example1.jpg', // name
'image/jpeg', // type
100000, // size
0 // error
);
Retrieve a stream representing the uploaded file.
StreamInterface
Example:
$stream = new Stream(fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+'));
$uploadedFile = new UploadedFile($stream);
$stream2 = $uploadedFile->getStream();
echo $stream2->getMetadata('mode');
// Outputs: r+
Move the uploaded file to a new location.
string targetPath * Path to which to move the uploaded file.
$stream = new Stream(
fopen(BOOTSTRAP_DIR . '/sample/shieldon_logo.png', 'r+')
);
$uploadedFile = new UploadedFile($stream);
$uploadedFile->moveTo('/home/terrylin/public/image_cache/shieldon_logo_png');
if (
file_exists('/home/terrylin/public/image_cache/shieldon_logo_png') &&
! file_exists(BOOTSTRAP_DIR . '/sample/shieldon_logo.png')
) {
echo 'File has been moved to the new place.';
} else {
echo 'Cannot move file.';
}
// Outputs: File has been moved to the new place.
Retrieve the file size.
int|null
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
echo $uploadedFile->getSize();
// Outputs: 100000
Retrieve the error associated with the uploaded file.
int
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
$uploadedFile->getError();
// Outputs: 0
Retrieve the filename sent by the client.
string|null
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
$uploadedFile->getClientFilename();
// Outputs: example1.jpg
Retrieve the media type sent by the client.
string|null
Example:
$uploadedFile = new \Shieldon\Psr7\UploadedFile(
'/tmp/php200A.tmp',
'example1.jpg',
'image/jpeg',
100000,
0
);
$uploadedFile->getClientMediaType();
// Outputs: image/jpeg
string uri = "" The URI.
Example:
$uri = new \Shieldon\Psr7\Uri('https://www.example.com');
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://www.example.com'
);
echo $uri->getScheme();
// Outputs: https
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getAuthority();
// Outputs: terry:1234@example.com:8888
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getUserInfo();
// Outputs: terry:1234
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getUserInfo();
// Outputs: example.com
int|null
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://terry:1234@example.com:8888/phpMyAdmin/'
);
echo $uri->getUserInfo();
// Outputs: 8888
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://example.com/post/?p=113&foo=bar#yes-i-do'
);
echo $uri->getPath();
// Outputs: /post/
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://example.com/post/?p=113&foo=bar#yes-i-do'
);
echo $uri->getQuery();
// Outputs: p=113&foo=bar
string
Example:
$uri = new \Shieldon\Psr7\Uri(
'https://example.com/post/?p=113&foo=bar#yes-i-do'
);
echo $uri->getFragment();
// Outputs: yes-i-do
$scheme)string scheme * The scheme to use with the new instance.
static
Example:
echo $uri->getScheme();
// Outputs: https
$url = $uri->withScheme('http');
echo $uri->getScheme();
// Outputs: http
$user, $password)param string user * The user name to use for authority.
param string|null password = null The password associated with $user.
return static
Example:
echo $uri->getUserInfo();
// Outputs: terry:1234
$url = $uri->withUserInfo('jack', '5678');
echo $uri->getUserInfo();
// Outputs: jack:5678
$host)string host * The hostname to use with the new instance.
static
Example:
echo $uri->getUserInfo();
// Outputs: example.com
$url = $uri->withHost('terryl.in');
echo $uri->getHost();
// Outputs: terryl.in
$port)int|null port * The port to use with the new instance; a null value removes the port information.
static
Example:
echo $uri->getUserInfo();
// Outputs: 8888
$uri = $uri->withPort(443);
echo $uri->getUserInfo();
// Outputs: 443
$uri = $uri->withPort(null);
echo $uri->getUserInfo();
// Outputs:
$path)string path * The path to use with the new instance.
static
Example:
echo $uri->getPath();
// Outputs: /post/
$uri = $uri->withPath('/new-path');
echo $uri->getPath();
// Outputs: /new-path
$query)string query * The query string to use with the new instance.
static
Example:
echo $uri->getQuery();
// Outputs: p=113&foo=bar
$uri = $uri->witQuery('p=120&foo=baz');
echo $uri->getQuery();
// Outputs: p=120&foo=baz
$fragment)string fragment * The fragment to use with the new instance.
static
Example:
echo $uri->getFragment();
// Outputs: yes-i-do
$uri = $uri->withFragment('no-i-cant');
echo $uri->getFragment();
// Outputs: no-i-cant
string
Example:
$uri = new Uri('http://example.com:8888/demo/#section-1');
echo $uri;
// Outputs: http://example.com:8888/demo/#section-1
composer install
composer test
Shieldon HTTP message library (PSR-7 implementation) is brought to you by Terry L. from Taiwan.
Shieldon HTTP message library is an open-sourced software licensed under the MIT license.