Http request object

Swoft's request and response are implemented in the PSR 7 specification. Request and response objects exist in every HTTP request.

  • Request object Request is Swoft\Http\Message\Request
  • The response object Response is Swoft\Http\Message\Response

note!
According to the immutable of the PSR-7 object, all the with* methods are cloned and returned, and must receive new objects for further processing, or use chained calls.

basic method

The PSR-7 interface provides these public methods for request and response objects:

  • withProtocolVersion($version)
  • withHeader($name, $value)
  • withAddedHeader($name, $value)
  • withoutHeader($name)
  • withBody(StreamInterface $body)

The PSR-7 interface provides these methods for request objects:

  • withMethod(string $method)
  • withUri(UriInterface $uri, $preserveHost = false)
  • withCookieParams(array $cookies)
  • withQueryParams(array $query)
  • withUploadedFiles(array $uploadedFiles)
  • withParsedBody($data)
  • withAttribute($name, $value)
  • withoutAttribute($name)

See PSR-7 for more details and see the specific implementation classes in swowt/http-message

How to get

  • Parameter injection via controller method (Request $request)
  • Get Swoft\Context\Context::mustGet()->getRequest() by request context

Request action method

 $request = \Swoft\Context\Context::mustGet()->getRequest();
$method = $request->getMethod(); 

Requested URI

Each HTTP request has a URI that identifies the requested application resource. The HTTP request URI has several parts:

  • Scheme (eg http or https )
  • Host (eg example.com )
  • Port (eg 80 or 443 )
  • Path (eg /users/1 )
  • Query string (eg sort=created&dir=asc )

You can get the PSR-7 URI object by requesting the object's getUri() method:

 $uri = $request->getUri(); 

The URI of the PSR-7 request object is itself an object that provides the following methods to check the URL portion of the HTTP request:

  • getScheme()
  • getAuthority()
  • getUserInfo()
  • getHost()
  • getPort()
  • getPath()
  • getQuery() (eg a=1&b=2 )
  • getFragment()

Request Headers

All Headers

 $headers = $request->getHeaders();

foreach ($headers as $name => $values) {
    echo $name . ": " . implode(", ", $values).PHP_EOL;
} 

Specified Header

  • The return value is array
 $headerValueArray = $request->getHeader('host');
print_r($headerValueArray); 
  • The return value is a string
 $host = $request->getHeaderLine("host");
print_r($host); 

Request data acquisition

GET data

 $data = $request->query();
$some = $request->query('key', 'default value')
$data = $request->get();
$some = $request->get('key','default value'); 

POST data

 $data = $request->post();
$some = $request->post('key', 'default value') 

GET & POST data

 $data = $request->input();
$some = $request->input('key', 'default value') 

JSON data

Only valid when Content-Type is application/json

 $data = $request->json();
$some = $request->json('key', 'default value') 

RAW data

 $data = $request->raw(); 

SERVER data

 $data = $request->getServerParams();
$some = $request->server('key', 'default value') 

Get uploaded files

 $file = $request->getUploadedFiles(); 

The result obtained is a one-dimensional array or a two-digit array, and the data structure is as follows. If the single file uploaded in the form is a one-dimensional array, the contents of the array are Swoft\Http\Message\Upload\UploadedFile file objects. For example, if the file field name is file , the data structure is

 array(1) {
  ["file"]=>
  object(Swoft\Http\Message\Upload\UploadedFile)#6510 (7) {
    ["size":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    int(1319)
    ["errorCode":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    int(0)
    ["file":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    string(25) "/tmp/swoole.upfile.f7p2EL"
    ["clientFilename":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    string(6) "at.png"
    ["clientMediaType":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    string(9) "image/png"
    ["moved":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    NULL
    ["path":"Swoft\Http\Message\Upload\UploadedFile":private]=>
    NULL
  }
} 

If the form is an array of fields, uploading multiple files, such as file[] , returns a two-dimensional array. The contents of the array are still Swoft\Http\Message\Upload\UploadedFile file objects. The data structure is as follows:

 array(1) {
  ["file"]=>
  array(2) {
    [0]=>
    object(Swoft\Http\Message\Upload\UploadedFile)#6516 (7) {
      ["size":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      int(1319)
      ["errorCode":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      int(0)
      ["file":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      string(25) "/tmp/swoole.upfile.TVKdOS"
      ["clientFilename":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      string(6) "at.png"
      ["clientMediaType":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      string(9) "image/png"
      ["moved":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      NULL
      ["path":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      NULL
    }
    [1]=>
    object(Swoft\Http\Message\Upload\UploadedFile)#6510 (7) {
      ["size":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      int(5489)
      ["errorCode":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      int(0)
      ["file":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      string(25) "/tmp/swoole.upfile.XS2vQg"
      ["clientFilename":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      string(8) "deal.png"
      ["clientMediaType":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      string(9) "image/png"
      ["moved":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      NULL
      ["path":"Swoft\Http\Message\Upload\UploadedFile":private]=>
      NULL
    }
  }
} 

File operation method

  • moveTo() moves the uploaded file to a new location.
  • getSize() Get the file size in byte .
  • getError() gets the error information related to the uploaded file. If there is no error, it will have to return the UPLOAD_ERR_OK constant. If it is wrong, it will return the UPLOAD_ERR_XXX related constant.
  • getClientFilename() gets the file name of the client local when the file is uploaded. Don't trust the value returned by this method. The client may send a malicious fake filename with the intent to destroy or crack your application.
  • getClientMediaType() gets the MediaType type of the file in the client, don't trust the value returned by this method. The client may send a malicious fake filename with the intent to destroy or crack your application.

Some auxiliary methods

  • XHR
 if ($request->isAjax()) {
    // Do something
}
if ($request->isXmlHttpRequest()) {
    // Do something
} 
  • GET
 if ($request->isGet()) {
    // Do something
} 
  • POST
 if ($request->isPost()) {
    // Do something
} 
  • PUT
 if ($request->isPut()) {
    // Do something
} 
  • DELETE
 if ($request->isDelete()) {
    // Do something
} 
  • PATCH
 if ($request->isPatch()) {
    // Do something
} 
  • Content-Type
 $contentType = $request->getContentType(); 
/docs/2.x/en/http-server/request.html
progress-bar