sFire PHP Framework

HTTP Response

The sFire\HTTP\Response object can be used to add headers and files into the response for the client.

In this section we will handle:

  • How to add custom headers to the response
  • How to remove headers from the response
  • How to add a file to the response
  • How to set a response status (code)

Add a response header

To add a single header to the response, you can call the addHeader method. This method accepts three parameters.The first parameter is the header you would like to set while the second parameter is the value of this header. The third parameter is an optional Integer which will set the HTTP status code.

Parameters
Response :: addHeader(String $header, String $value, [Integer $code]);
Example
Response :: addHeader('Keep-alive', 'timeout=15, max=100');
Response :: addHeader('Location', 'https://sfire.nl', 302);

Remove headers

To remove a single header you previously defined, you may use the removeHeader method. This method accepts one String parameter.

Example
Response :: removeHeader('Keep-alive');

To remove all headers, you can use the removeHeaders method.

Example
Response :: removeHeaders();

This will remove all previously defined headers from the response;

Add file to the response

If you want to give the client a file, you may do so by calling the file method. This method accepts one parameter which is a sFire File object.

Parameter
Response :: file(sFire\System\File $file);
Example
use sFire\System\File;
use sFire\HTTP\Response;

$file = new File('foo.txt');

Response :: file($file);

Set response status code

To set a status code, you can use the setStatus method. This method accepts one parameter which is an Integer that is defined in the supported HTTP status codes which you can see below.

Response :: setStatus(404); //Similar to setting a header "HTTP/1.1 404 Not Found"

The server HTTP protocol version will be used. If non available, HTTP/1.0 will be used.

HTTP status codes

Below is a list of the supported HTTP status codes.

Status code Status text
100 Continue
101 Switching Protocols
102 Processing
200 OK
201 Created
202 Accepted
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content
207 Multi-Status
208 Already Reported
226 IM Used
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other
304 Not Modified
305 Use Proxy
306 Switch Proxy
307 Temporary Redirect
308 Permanent Redirect
400 Bad Request
401 Unauthorized
402 Payment Required
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict
410 Gone
411 Length Required
412 Precondition Failed
413 Payload Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Satisfiable
417 Expectation Failed
418 I'm a teapot
419 Authentication Timeout
420 Method Failure
421 Misdirected Request
422 Unprocessable Entity
423 Locked
424 Failed Dependency
426 Upgrade Required
428 Precondition Required
429 Too Many Requests
431 Request Header Fields Too Large
440 Login Timeout
444 No Response
449 Retry With
450 Blocked by Windows Parental Controls
451 Unavailable For Legal Reasons
494 Request Header Too Large
495 Cert Error
496 No Cert
497 HTTP to HTTPS
498 Token expired/invalid
499 Client Closed Request
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates
507 Insufficient Storage
508 Loop Detected
509 Bandwidth Limit Exceeded
510 Not Extended
511 Network Authentication Required
520 Unknown Error
522 Origin Connection Time-out
598 Network read timeout error
599 Network connect timeout error