Sending HTTP Request Using cURL Set-1

01 Aug, 2021
Whenever we are dealing with HTTP requests, cURL simplifies our tasks to a great extent and is the easiest tool to get our hands dirty on.
cURL: It stands for “client URL” and is used in command line or scripts to transfer data. It is a great tool for dealing with HTTP requests like GET, POST, PUT, DELETE, etc. Although it provides us with the support of other internet protocols like HTTPS, FTP, SMTP, TELNET, we will be limiting it to HTTP in this article.
Pre-requisite: Install cURL properly as per your underlying operating system. 
To check if it’s installed in your system or to know its version, the following command is executed in the command prompt
Syntax:
 curl --version
Output:
curl 7.67.0 (x86_64-pc-linux-gnu) libcurl/7.67.0 OpenSSL/1.1.1d zlib/1.2.11  brotli/1.0.7 libidn2/2.2.0 libpsl/0.20.2 (+libidn2/2.0.5) libssh2/1.8.0 nghttp2/1.41.0 librtmp/2.3 Release-Date: 2019-11-06 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp scp sftp smb smbs smtp smtps telnet tftp Features: AsynchDNS brotli GSS-API HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM NTLM_WB PSL SPNEGO SSL TLS-SRP UnixSockets 
We can see that the output states the version, release date, protocols, and other features of curl as –version flag was used.
Note: The output may differ based on the version and the underlying operating system.
GET request using cURL: Get request is the most commonly used HTTP request as it is used to request the data from the server about a particular target i.e website. Let’s start by executing a simple Get request.
 curl http://138.68.158.87:30954/login.php
Note that instead of http://138.68.158.87:30954/login.php, you can specify the target on which you want to request the data.
Example :
HTML
<!DOCTYPE html><html lang="en"> <head>    <meta charset="UTF-8">    <link rel="stylesheet" href="./style.css"></head> <body>    <hgroup>        <h1>Admin panel</h1>    </hgroup>     <form action="" method="post">        <div class="group">            <input name="username" type="text">            <span class="highlight"></span>            <span class="bar"></span>            <label>Username</label>        </div>         <div class="group">            <input name="password" type="password">            <span class="highlight"></span>            <span class="bar"></span>            <label>Password</label>        </div>         <button type="submit" class="button buttonBlue">            Login            <div class="ripples buttonRipples">                <span class="ripplesCircle"></span>            </div>        </button>    </form>     <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'>    </script>     <script src="./script.js"></script></body> </html>
In this manner, you will get the entire output which will be the same if your query through the browser and the response from the server is rendered by your client browser and then shown to you in a simplified manner.
Note: If you will view the target source code, you will find the same output.
Many other flags can be used with the above query.  
-v: It is used to get verbose output.  
curl http://138.68.158.87:30954/login.php -v
-u: Server user and password.  
 curl -u username:password http://138.68.158.87:30954/login.php -v
-L: Follow redirects.  
curl -u username:password -L http://138.68.158.87:30954/login.php -v
-X: Specify request command to use.  
curl -X GET http://138.68.158.87:30954/login.php -v
Note: By default curl uses GET request if we don’t specify the request command.
-s: Silent mode.  
curl -u username:password -s -L http://138.68.158.87:30954/login.php -v
You can dive deeply into using different flags as per the need with the help of the -h flag.
curl -h
 
umangthakkar
PHP-cURL
PHP-Questions
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.