What is HTTP?
In this tutorial, you will learn the HTTP request method in PHP. Hypertext Transfer Protocol (HTTP) is a client and server communication protocol. It enables the communication between server and client using the request-response protocol. For instance, the client sends a request to the server and the server return response that contains a status code along with request information.
The HTTP protocol allows the following methods to send requests to the server.
Out of the above HTTP methods, GET and POST methods are most common and useful. In this PHP tutorial, you will learn about GET and POST methods in PHP.
HTTP GET request Method in PHP
The GET method sends data to a specific resource. Get method uses a Query string (key/value pair of data) to send data in the URL.
Properties of GET method
- Can cache by memory.
- Remain in the browser history.
- We can bookmark it.
- Not recommended when dealing with sensitive data.
- Specific length restrictions. It can handle maximum 1024 characters.
- Only used to request data. Cannot modify.
In PHP $_GET is a global array containing the get requests information in the form of key-value pair (associative array).
HTTP POST request Method in PHP
POST is the most useful request method in HTTP protocol. POST method sends the data sent in the request body. This method creates and updates resources.
Properties of POST method
- Never cache request in memory.
- Do not remain in the browser history.
- It cannot be bookmarked.
- There is no restrictions on data length.
- It is more secure to send the data as compared to GET method.
In PHP $_POST is a global array containing post requests information in form of key-value pair (associative array).
HTTP PUT request Method in PHP
Both PUT and POST methods are used to Create or Update resources on the server. However, there is a difference between the POST and PUT methods. Sending POST requests, again and again, will create the same resources multiple times. On the other hand, the PUT method produces the same result upon sending requests again and again.
HEAD request Method in PHP
The HEAD method works similarly to the GET method as both are used to get resources. However, the HEAD method is different from GET method in that it does not return the resource in the response. For example, making a GET request /users will send a list of users in response. But making a HEAD request /users will return will not return a list of users.
The HEAD request is basically used to check what will be returned in the response. Such as before downloading a large file or getting a big response.
DELETE request Method in PHP
DELETE request method removes some specific resources.
OPTIONS request Method in PHP
OPTIONS method specifies the communication options for the target resource.
In the next section, we will compare the GET and POST methods as these are mostly used HTTP methods by the developers. You should know which method is suitable to use. After moving towards the advanced PHP tutorials, this content will help you to develop GET and POST form data with more understanding.
When to use GET and POST?
The GET method sends the request data as a part of the request URL. It is clear from here that, if you are on a sensitive platform where data cannot be exposed to the world, the GET method is never used. The GET method has a limit of a maximum of 2000 characters in the URL. Hence, request data over this limit is not entertained by this method.
The POST method is much more safer and secure as the data is sent using the request body. In this way, it is not visible to anyone and hence, cannot be modified. Also, there is no limit to data, POST method can be used for making both small and large size requests. As the GET method uses a URL to send data, so it can be bookmarked. But, bookmarking the POST requests is not possible.
Super Global variables in PHP
Super global variables in PHP are accessible from anywhere and store specific information in them. We have the following list of super global variables in PHP.
|$GLOBALS||Contains all global variables. These variables are always available at any scope.|
|$_REQUEST||Used to get data after submitting HTML form.|
|$_COOKIE||Contains all the cookies in the current script.|
|$_POST||Collect data from HTML form when method=post|
|$_GET||Collect data from HTML form when method=get|
|$_FILES||Associative array of items uploaded to the current script using method=post|
|$_ENV||Environment variables are available for the current script.|
|$_SESSION||Store session variables for the current session.|
|$_SERVER||Store information about Header, Path, and Script locations.|
One thing that makes JSON objects highly useable is that it is compatible with almost every server-side and client-side language. Owing to this, developers can make requests and get responses by using a standard JSON format.
For official reference to PHP requests click here.