How can we make get/post requests using PHP

Udara Kurukulasooriya
5 min readFeb 23, 2021

--

PHP/ by Udara Kurukulasooriya/ February 16,2021

This guide shows you how to make HTTP requests. There are various methods to send them. In this article I’m going to discuss about GET and POST methods. We might need additional libraries such as cURL for the steps I am going to follow.

Execution of a POST request

Both GET and POST methods are used to transfer data from client to server in HTTP protocol. From this guide we are going to observe different methods of sending data through PHP using GET and POST methods and some fundamental differences between them.

The GET method requests a representation of the specified resource.

GET requests :

  • Only to request a resource it should be used.
  • parameters are shown in the URL
  • can be cached
  • remains in the browser history
  • can be bookmarked
  • Not ideal when dealing with sensitive data
  • have some length limits
  • Can only send up to 1024 characters.
<?php
if( $_GET["name"] || $_GET["age"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";

exit();
}
?>
<html>
<body>

<form action = "<?php $_PHP_SELF ?>" method = "GET">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>

</body>
</html>

The above code will generate the following result.

cURL

Before we go any further let me give you a brief introduction about cURL library.

cURL stands for ‘Clients for URLs’ and is a PHP library and a command line tool (like wget) that helps you send files and also download data over HTTP and FTP. It supports proxies, you can transfer data over SSL connections, you can set cookies and even get files that are behind a login.

cURL is a powerful library that supports many different protocols, options, and provides detailed information about the URL requests.

GET requests with cURL

curl -X GET \
-H "Content-type: application/json" \
-H "Accept: application/json" \
-d '{"param0":"pradeep"}' \
"http://server:5050/a/c/getName"

Above code can be used to submit GET request with JSON including in the body. Most modern web servers accepts these types of requests.

The HTTP POST method sends data to the server. It is often used when uploading a file or when submitting a completed web form.

POST requests:

  • Usually used to create a resource
  • parameters won’t get displayed in the URL
  • Never cached
  • do not remain in the browser history
  • cannot be bookmarked
  • Secure compared to GET. Doesn’t pass parameters via URL.
  • have no length limits
<?php$url = 'http://server.com/path';$data = array('key1' => 'value1', 'key2' => 'value2');// Even if you send the request to https:// use key 'http'//$options = array('http' => array('header'  => "Content-type: application/x-www-form-urlencoded\r\n",'method'  => 'POST','content' => http_build_query($data),),);$context  = stream_context_create($options);$result = file_get_contents($url, false, $context);var_dump($result);

The above code segment also outputs the same result as above in get method.

Even though we can avoid the ‘shoulder surfing’ by using POST instead of GET , in terms of encrypted communication both methods hold the same amount of assurance.

POST requests with cURL

From following command we can make a basic POST request using cURL.

We can send data with the POST request by using the -d flag. In the below example, we send a user and a pass field along with their corresponding values.

We can use the -H flag to send a specific data type or header with cURL. In the below example, we send a JSON object with the request.

Complete files can also be sent in the command-line using cURL. Take a look at the following example.

Using Pecl_Http

Additionally we can use “Pecl_Http” for posting. Let’s briefly discuss about this with some examples.

Pecl_Http has two interfaces — one procedural and the other one object-oriented; we’ll start by looking at the former. This happens to be even simpler than in cURL.

This extension has a method to expressly post a request, and it has the ability to optionally accept data to go with it, extremely simple and easy.

Using Pecl_Http: the OO interface

Next, we will see what the OO version of the extension looks like. Exactly the same call as the above example, but using the alternative interface, means our code will be changed to something like this:

This example is a little lengthier than the previous one, and someone might think this implies this approach is far more complex. In some senses that may be true and its probably too much for our extremely simple example. However I have to mention that the pecl_http extension is very flexible and powerful, and can be used to handle some cases that the curl extension is unable to handle. So even if it looks more complex, it could still be an awesome choice to implement.

Conclusion

And that’s how GET and POST methods work on PHP. When dealing with cases that cURL cannot handle, we can use pecl_http. GET parameters are passed through URL hence we should never use it for secure applications. While POST method has it’s own flaws it doesn’t expose information through URL. If the privacy is a big concern, the best secure method is using secure http.

Thank you for reading

--

--