Programming PHP What is cURL and how to use PHP cURL?

What is cURL and how to use PHP cURL?

What is cURL and how to use PHP cURL?

The cURL is an abbreviation for ‘Client for URLs’. This tutorial will explain to you about What is cURL and how to use PHP cURL. Basically it is spelled with URL in uppercase that signify that it deals with URLs. The correct pronunciation of cURL is ‘see URL’. The cURL project has two products libcurl and curl.

What is libcurl in cURL?

libcurl is a free and easily usable client-side URL transfer library. It supports HTTP, HTTPS, FTP, TPS, GOPHER, TELNET, DICT, FILE, and LDAP. libcurl also supports TTPS certificates, HTTP POST, HTTP PUT, FTP uploading and Kerberos. Moreover, it provides support for HTTP based upload, proxies, cookies, user & password authentication, file transfer resume, HTTP proxy tunneling and many more. libcurl is also thread-safe, IPv6 compatible, feature-rich, well supported and fast.

What is curl in cURL?

curl: This is a command-line tool for sending or receiving files by using URL syntax. As curl uses libcurl, it supports a wide range of common internal protocols.  currently it includes HTTP, HTTPS, FTP, FTPS, GOPHER, TELNET, DICT, and FILE.

What is PHP/cURL?

PHP/cURL is a module for PHP that enables PHP programs to access curl functions within PHP. If cURL support is enabled in PHP, the phpinfo() function will display in its output. You should always check for it before writing any cURL program in PHP. If the cURL module is not enabled, the program will not run. To check this, simply create a PHP file and paste the code below and browse that page.

In simple words, cURL is a PHP library with the help of which we can make HTTP requests in PHP. We can use cURL to fetch data from a URL. It is very much used in API handling because in the API we get or post data only with the help of URL. To use cURL, you must have a libcurl library added to your server which is normally present by default in all servers.

How to use cURL in PHP

Like CURLOPT_URL, you can use multiple setopt functions to set n number of options. There is a wide list of options available, you can see a complete list of options from here – set opt ​​list

How to use cURL in PHP step by step

  1. Firstly,  initialize the session of cURL by using curl_init() function.
  2. After this, you need to set the options with the help of curl_setopt() function. This depends on the requirements, what and how many options are to be set.
  3. If you are fetching Data then make CURLOPT_POST false and remove CURLOPT_POSTFIELDS also. If you want to insert DATA i.e. want to POST your request, then pass your value in data of CURLOPT_POSTFIELDS.
  4. Finally, you have to execute cURL with the help of curl_exec () function
  5. And after execution, you have to close the cURL connection by using of curl_close () function

This is how you can use cURL in PHP.

Some important curl options

curl_setopt($scurl, CURLOPT_URL, “http://localhost/phplab/api/books/post.php”);
curl_setopt($scurl, CURLOPT_POST, true);
curl_setopt($scurl, CURLOPT_POSTFIELDS, $data);
curl_setopt($scurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($scurl, CURLOPT_CUSTOMREQUEST, “PUT”);
curl_setopt($scurl, CURLOPT_CUSTOMREQUEST, “DELETE”);

Leave a Reply