Working With HTTP Requests

In order to get the most out of CodeIgniter, you need to have a basic understanding of how HTTP requestsand responses work. Since this is what you work with while developing web applications, understanding theconcepts behind HTTP is a must for all developers that want to be successful.

The first part of this chapter gives an overview. After the concepts are out of the way, we will discusshow to work with the requests and responses within CodeIgniter.

What is HTTP?

HTTP is simply a text-based convention that allows two machines to talk to each other. When a browserrequests a page, it asks the server if it can get the page. The server then prepares the page and sendsa response back to the browser that asked for it. That’s pretty much it. Obviously, there are some complexitiesthat you can use, but the basics are really pretty simple.

HTTP is the term used to describe that exchange convention. It stands for HyperText Transfer Protocol. Your goal whenyou develop web applications is to always understand what the browser is requesting, and be able torespond appropriately.

The Request

Whenever a client (a web browser, smartphone app, etc) makes a request, it sends a small text messageto the server and waits for a response.

The request would look something like this:

  1. GET / HTTP/1.1
  2. Host codeigniter.com
  3. Accept: text/html
  4. User-Agent: Chrome/46.0.2490.80

This message displays all of the information necessary to know what the client is requesting. It tells themethod for the request (GET, POST, DELETE, etc), and the version of HTTP it supports.

The request also includes a number of optional request headers that can contain a wide variety ofinformation such as what languages the client wants the content displayed as, the types of formats theclient accepts, and much more. Wikipedia has an article that lists all header fields if you want to look it over.

The Response

Once the server receives the request, your application will take that information and generate some output.The server will bundle your output as part of its response to the client. This is also represented asa simple text message that looks something like this:

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.8.0
  3. Date: Thu, 05 Nov 2015 05:33:22 GMT
  4. Content-Type: text/html; charset=UTF-8
  5.  
  6. <html>
  7. . . .
  8. </html>

The response tells the client what version of the HTTP specification that it’s using and, probably mostimportantly, the status code (200). The status code is one of a number of codes that have been standardizedto have a very specific meaning to the client. This can tell them that it was successful (200), or that the pagewasn’t found (404). Head over to IANA for a full list of HTTP status codes.

Working with Requests and Responses

While PHP provides ways to interact with the request and response headers, CodeIgniter, like most frameworks,abstracts them so that you have a consistent, simple interface to them. The IncomingRequest classis an object-oriented representation of the HTTP request. It provides everything you need:

  1. use CodeIgniter\HTTP\IncomingRequest;
  2.  
  3. $request = new IncomingRequest(new \Config\App(), new \CodeIgniter\HTTP\URI());
  4.  
  5. // the URI being requested (i.e. /about)
  6. $request->uri->getPath();
  7.  
  8. // Retrieve $_GET and $_POST variables
  9. $request->getVar('foo');
  10. $request->getGet('foo');
  11. $request->getPost('foo');
  12.  
  13. // Retrieve JSON from AJAX calls
  14. $request->getJSON();
  15.  
  16. // Retrieve server variables
  17. $request->getServer('Host');
  18.  
  19. // Retrieve an HTTP Request header, with case-insensitive names
  20. $request->getHeader('host');
  21. $request->getHeader('Content-Type');
  22.  
  23. $request->getMethod(); // GET, POST, PUT, etc

The request class does a lot of work in the background for you, that you never need to worry about.The isAJAX() and isSecure() methods check several different methods to determine the correct answer.

Note

The isAJAX() method depends on the X-Requested-With header, which in some cases is not sent by default in XHR requests via JavaScript (i.e. fetch). See the AJAX Requests section on how to avoid this problem.

CodeIgniter also provides a Response class that is an object-oriented representationof the HTTP response. This gives you an easy and powerful way to construct your response to the client:

  1. use CodeIgniter\HTTP\Response;
  2.  
  3. $response = new Response();
  4.  
  5. $response->setStatusCode(Response::HTTP_OK);
  6. $response->setBody($output);
  7. $response->setHeader('Content-type', 'text/html');
  8. $response->noCache();
  9.  
  10. // Sends the output to the browser
  11. $response->send();

In addition, the Response class allows you to work the HTTP cache layer for the best performance.