Download the PHP package jason-gao/http without Composer
On this page you can find all versions of the php package jason-gao/http. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package http
Nette HTTP Component
Introduction
HTTP request and response are encapsulated in Nette\Http\Request
and Nette\Http\Response
objects which offer comfortable API and also act as
sanitization filter.
Documentation can be found on the website.
Installation
The recommended way to install is via Composer:
It requires PHP version 5.6 and supports PHP up to 7.2. The dev-master version requires PHP 7.1.
HTTP Request
Nette cleans out data sent by user from control and invalid characters.
The URL of the request is available as [api:Nette\Http\UrlScript] instance:
Determine current HTTP method:
Is the connection encrypted (HTTPS)?
Is this an AJAX request?
What is the user's IP address?
What URL the user came from? Returned as [Nette\Http\Url |urls] object.
Request parameters:
Uploaded files are encapsulated into [api:Nette\Http\FileUpload] objects:
HTTP headers are also accessible:
A useful method is detectLanguage()
. You can pass it an array with languages supported by application and it returns the one preferred by browser.
It is not magic, the method just uses the Accept-Language
header.
RequestFactory and URL filtering
Object holding current HTTP request is created by [api:Nette\Http\RequestFactory]. Its behavior can be modified. It's possible to clean up URLs from characters that can get into them because of poorly implemented comment systems on various other websites by using filters:
And then we let the factory generate a new httpRequest
and we store it in a system container:
HTTP response
Whether it is still possible to send headers or change the status code tells the isSent()
method. If it returns true,
it won't be possible to send another header or change the status code.
In that case, any attempt to send header or change code invokes Nette\InvalidStateException
. .[caution]
[Response status code | http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10] can be sent and retrieved this way:
For better source code readability it is recommended to use predefined constants instead of actual numbers:
Method setContentType($type, $charset=null)
changes Content-Type
response header:
Redirection to another URL is done by redirect($url, $code=302)
method. Do not forget to terminate the script afterwards!
To set the document expiration date, we can use setExpiration()
method. The parameter is either text data, number of seconds or a timestamp:
Now we send the HTTP response header:
Sent headers are also available:
There are two methods for cookie manipulation: setCookie()
and deleteCookie()
.
These two methods can take more parameters: $path
(subdirectory where the cookie will be available),
$domain
and $secure
. Their detailed description can be found in PHP manual for [php:setcookie] function.