Post Requests with cURL

A bunch of tools are necessary for a programmer to work efficiently. The so-called cURL is one of those crucial tools. This snippet is aimed at representing to you how to make Post requests with cURL accurately.

Watch a course Learn object oriented PHP

Making a Post Request with cURL

Many APIs support both POST and PUT requests that are similar to each other. Using cURL at the line of command makes it much easier to apply the form urlencoded format rather than JSON.

Here is a clear example of using cURL for a POST request:

<?php

//open connection
$ch = curl_init();

//set the url
curl_setopt($ch, CURLOPT_URL, $url);

//set the number of POST vars
curl_setopt($ch, CURLOPT_POST, count($fields));

//set POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

What is cURL

The cURL tool is generally applied for the transfer of data with Internet protocols for a particular URL.

Curl is considered a client-side program. Its name stands for client and URL. cURL encompasses a curl command line along with a libcurl library.

In the framework of this snippet, emphasized the curl command line. Curl is capable of working with multiple internet Protocols such as FTP, HTTP, TELNET and more.