Home » PHP cURL

PHP cURL

What is the PHP cURL?

cURL stands for the client URL. PHP cURL is a library that is the most powerful extension of PHP. It allows the user to create the HTTP requests in PHP. cURL library is used to communicate with other servers with the help of a wide range of protocols.

cURL allows the user to send and receive the data through the URL syntax. cURL makes it easy to communicate between different websites and domains.

cURL is divided into two parts: cURL and libcURL.

  1. cURL: cURL allows the user to send and receive the data through the URL syntax.
  2. libcURL: libcURL is a library. It was created by Daniel Stenberg. The cURL library is used to communicate with other servers with the help of a wide range of protocols. Currently, it supports a wide range of protocols, including LDAP, TPS, GOPHER, FTP, HTTP, HTTPS, FILE, HTTP POST, TELNET, DICT, HTTP PUT, FTP uploading, FTP resume, Kerberos, HTTP based upload, TTPS certificates, proxies, cookies, HTTP proxy tunneling, user & password authentication, etc.

Some Basic cURL Functions

S. No Function Name Description
1. curl_init() This function starts a cURL session.
2. curl_setopt() This function defines a various option for the cURL session.
3. curl_setopt($ch, option, value) This function defines the value and option for a cURL session by the “ch” parameter.
4. curl_exec() This function executes the cURL session, actual data transfer, and http request.
5. curl_close() This function is used to close a cURL session.

Basic Examples

Before we move on to more complicated examples, let’s review the basic example of a cURL request in PHP.

Example 1:

Example 2:

Example 3:

Other cURL Functions

S. No Function name Description
1. curl_copy_handle() This function is used to copy a cURL handle with all its preferences.
2. curl_errno() This function returns the last error number from the cURL session.
3. curl_error() This function returns the string containing the last error for the present session.
4. curl_escape() This function is used to encode the URL of the given string.
5. curl_file_create() This function is used to create a cURL file object.
6. curl_getinfo() This function returns the information of the specific transfer.
7. curl_multi_add_handle() This function connects the cURL_handle to the cURL_multi_handle.
8. curl_multi_close() This function is used to close multiple sets of cURL handles.
9. curl_multi_info_read() This function receives current transfer information.
10. curl_multi_exec() This function executes the sub-connections of the present cURL session.
11. curl_setopt_array() This function sets the multiple options for the cURL session.
12. curl_version This function returns the cURL version information.
13. curl_strerror() This function returns the string describing the given error.
14. curl_pause() This function is used to pause a cURL connection.
15. curl_reset() This function is used to reset all options of the cURL session.

How does the cURL extension work?

It runs by sending a request to the website, and it includes the following parts:

  1. Initialization
  2. Setting the options (for example, an option that defines the URL)
  3. Execution with curl_exec()
  4. Close the cURL handle

How to download a file from a remote site using PHP cURL?

If the user includes the CURLOPT_ FILE function in the server, the user can download the remote file from anywhere.

Example:

How to handle cookies with cURL?

The cookies are used to identify authenticated users on the website. For this, PHP cURL provides you with a mechanism by which you can save cookies.

There are two main function for handling the cookies

  1. CURLOPT_COOKIEJAR: This function defines the file required to write cookies.
  2. CURLOPT_COOKIEFILE: This function defines that file which is required to read the cookies.

Example:


Next TopicPilot Testing

You may also like