在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:nette/http开源软件地址:https://github.com/nette/http开源编程语言:PHP 99.2%开源软件介绍:Nette HTTP ComponentIntroductionHTTP request and response are encapsulated in Documentation can be found on the website. Support MeDo you like Nette DI? Are you looking forward to the new features? Thank you! Installationcomposer require nette/http It requires PHP version 8.0 and supports PHP up to 8.1. HTTP RequestAn HTTP request is an Nette\Http\Request object. What is important is that Nette when creating this object, it clears all GET, POST and COOKIE input parameters as well as URLs of control characters and invalid UTF-8 sequences. So you can safely continue working with the data. The cleaned data is then used in presenters and forms. Class withUrl(Nette\Http\UrlScript $url): Nette\Http\RequestReturns a clone with a different URL. getUrl(): Nette\Http\UrlScriptReturns the URL of the request as object [UrlScript|urls#UrlScript]. $url = $httpRequest->getUrl();
echo $url; // https://nette.org/en/documentation?action=edit
echo $url->getHost(); // nette.org Browsers do not send a fragment to the server, so getQuery(string $key = null): string|array|nullReturns GET request parameters: $all = $httpRequest->getQuery(); // array of all URL parameters
$id = $httpRequest->getQuery('id'); // returns GET parameter 'id' (or null) getPost(string $key = null): string|array|nullReturns POST request parameters: $all = $httpRequest->getPost(); // array of all POST parameters
$id = $httpRequest->getPost('id'); // returns POST parameter 'id' (or null) getFile(string $key): Nette\Http\FileUpload|array|nullReturns upload as object Nette\Http\FileUpload: $file = $httpRequest->getFile('avatar');
if ($file->hasFile()) { // was any file uploaded?
$file->getName(); // name of the file sent by user
$file->getSanitizedName(); // the name without dangerous characters
} getFiles(): arrayReturns tree of upload files in a normalized structure, with each leaf an instance of Nette\Http\FileUpload: $files = $httpRequest->getFiles(); getCookie(string $key): string|array|nullReturns a cookie or $sessId = $httpRequest->getCookie('sess_id'); getCookies(): arrayReturns all cookies: $cookies = $httpRequest->getCookies(); getMethod(): stringReturns the HTTP method with which the request was made. echo $httpRequest->getMethod(); // GET, POST, HEAD, PUT isMethod(string $method): boolChecks the HTTP method with which the request was made. The parameter is case-insensitive. if ($httpRequest->isMethod('GET')) ... getHeader(string $header): ?stringReturns an HTTP header or $userAgent = $httpRequest->getHeader('User-Agent'); getHeaders(): arrayReturns all HTTP headers as associative array: $headers = $httpRequest->getHeaders();
echo $headers['Content-Type']; getReferer(): ?Nette\Http\UrlImmutableWhat URL did the user come from? Beware, it is not reliable at all. isSecured(): boolIs the connection encrypted (HTTPS)? You may need to [set up a proxy|configuring#HTTP proxy] for proper functionality. isSameSite(): boolIs the request coming from the same (sub) domain and is initiated by clicking on a link? isAjax(): boolIs it an AJAX request? getRemoteAddress(): ?stringReturns the user's IP address. You may need to [set up a proxy|configuring#HTTP proxy] for proper functionality. getRemoteHost(): ?stringReturns DNS translation of the user's IP address. You may need to [set up a proxy|configuring#HTTP proxy] for proper functionality. getRawBody(): ?stringReturns the body of the HTTP request: $body = $httpRequest->getRawBody(); detectLanguage(array $langs): ?stringDetects language. As a parameter // Header sent by browser: Accept-Language: cs,en-us;q=0.8,en;q=0.5,sl;q=0.3
$langs = ['hu', 'pl', 'en']; // languages supported in application
echo $httpRequest->detectLanguage($langs); // en RequestFactoryThe object of the current HTTP request is created by Nette\Http\RequestFactory. If you are writing an application that does not use a DI container, you create a request as follows: $factory = new Nette\Http\RequestFactory;
$httpRequest = $factory->fromGlobals(); RequestFactory can be configured before calling 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: // remove spaces from path
$requestFactory->urlFilters['path']['%20'] = '';
// remove dot, comma or right parenthesis form the end of the URL
$requestFactory->urlFilters['url']['[.,)]$'] = '';
// clean the path from duplicated slashes (default filter)
$requestFactory->urlFilters['path']['/{2,}'] = '/'; HTTP ResponseAn HTTP response is an Nette\Http\Response object. Unlike the Request, the object is mutable, so you can use setters to change the state, ie to send headers. Remember that all setters must be called before any actual output is sent. The setCode(int $code, string $reason = null)Changes a status response code. For better source code readability it is recommended to use predefined constants instead of actual numbers. $httpResponse->setCode(Nette\Http\Response::S404_NOT_FOUND); getCode(): intReturns the status code of the response. isSent(): boolReturns whether headers have already been sent from the server to the browser, so it is no longer possible to send headers or change the status code. setHeader(string $name, string $value)Sends an HTTP header and overwrites previously sent header of the same name. $httpResponse->setHeader('Pragma', 'no-cache'); addHeader(string $name, string $value)Sends an HTTP header and doesn't overwrite previously sent header of the same name. $httpResponse->addHeader('Accept', 'application/json');
$httpResponse->addHeader('Accept', 'application/xml'); deleteHeader(string $name)Deletes a previously sent HTTP header. getHeader(string $header): ?stringReturns the sent HTTP header, or $pragma = $httpResponse->getHeader('Pragma'); getHeaders(): arrayReturns all sent HTTP headers as associative array. $headers = $httpResponse->getHeaders();
echo $headers['Pragma']; setContentType(string $type, string $charset = null)Sends the header $httpResponse->setContentType('text/plain', 'UTF-8'); redirect(string $url, int $code = self::S302_FOUND): voidRedirects to another URL. Don't forget to quit the script then. $httpResponse->redirect('http://example.com');
exit; setExpiration(?string $time)Sets the expiration of the HTTP document using the // browser cache expires in one hour
$httpResponse->setExpiration('1 hour'); setCookie(string $name, string $value, $time, string $path = null, string $domain = null, bool $secure = null, bool $httpOnly = null, string $sameSite = null)Sends a cookie. The default values of the parameters are:
The time can be specified as a string or the number of seconds. $httpResponse->setCookie('lang', 'en', '100 days'); deleteCookie(string $name, string $path = null, string $domain = null, bool $secure = null): voidDeletes a cookie. The default values of the parameters are:
$httpResponse->deleteCookie('lang'); Uploaded FilesMethod The structure reflects the naming of elements in HTML. In the simplest example, this might be a single named form element submitted as: <input type="file" name="avatar"> In this case, the [
'avatar' => /* FileUpload instance */
] The $request->getFile('avatar')->hasFile(); In the case of an input using array notation for the name: <input type="file" name="my-form[details][avatar]"> returned tree ends up looking like this: [
'my-form' => [
'details' => [
'avatar' => /* FileUpload instance */
],
],
] You can also create arrays of files: <input type="file" name="my-form[details][avatars][] multiple"> In such a case structure looks like: [
'my-form' => [
'details' => [
'avatars' => [
0 => /* FileUpload instance */,
1 => /* FileUpload instance */,
2 => /* FileUpload instance */,
],
],
],
] The best way to access index 1 of a nested array is as follows: $file = Nette\Utils\Arrays::get(
$request->getFiles(),
['my-form', 'details', 'avatars', 1],
null
);
if ($file instanceof FileUpload) {
...
} Because you can't trust data from the outside and therefore don't rely on the form of the file structure, it's safer to use the
Overview of |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论