PHP - Client URL Functions



PHP cURL or Client URL functions are used to send HTTP requests and interact with web servers. They allow you communicate with several types of servers and send and receive data using a range of protocols, such as HTTP, HTTPS, FTP, and others.

These functions are essential to PHP programs for managing responses and send requests to the web. They come in particularly handy when interacting with APIs, downloading files, and scraping the web.

The two parts that create cURL are libcURL and cURL.

  • cURL: The cURL allows you to send and receive data using the URL syntax.
  • libcURL: This is a library. It was created by Daniel Stenberg. The cURL library enables communication with other servers using a variety of protocols. Currently, it supports a variety 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 tunnelling, user and password authentication, and so on.

Installation

To use PHP's cURL capability, you also need to compile PHP --with-curl[=DIR]. The directory where the lib and include folders are kept is called DIR. The curl folder, which needs to be present in the include directory, is where you should find the easy.h and curl.h files. There should be a file called libcurl.a in the lib directory.

Requirements

To use the cURL functions in PHP, the libcurl library must be installed:

  • Any version of PHP requires libcurl to be at least version 7.10.5.
  • PHP 7.3.0 or later requires libcurl version 7.15.5 or above.
  • To work with PHP 8.0.0 or later, libcurl must be 7.29.0 or higher.

Runtime Configuration

There are settings in the php.ini file that modify the way cURL works.

PHP cURL Constants

PHP cURL constants are useful predefined values you can use for setting up settings and get data. Here are some common constants:

CURLOPT Constants

The following constants and Curl_setopt() are used to set options for a cURL session:

Sr.No Constant & Description PHP Version
1 CURLOPT_URL

The URL to fetch.

3
2 CURLOPT_RETURNTRANSFER

If set to true, the response is returned as a string instead of being directly output.

4
3 CURLOPT_POST

If set to true, makes a POST request.

4
4 CURLOPT_POSTFIELDS

The data to send in a POST request.

4
5 CURLOPT_FOLLOWLOCATION

If set to true, follows any "Location:" header that the server sends.

4
6 CURLOPT_TIMEOUT

The maximum number of seconds to allow cURL functions to execute.

4
7 CURLOPT_SSL_VERIFYPEER

If set to false, stops cURL from verifying the peer's certificate.

4
8 CURLOPT_CAINFO

The path to the Certificate Authority (CA) bundle.

4

CURLINFO Constants

With curl_getinfo() and the following constants, you can get information about a cURL session:

Sr.No Constant & Description PHP Version
1 CURLINFO_HTTP_CODE

The HTTP response code.

4
2 CURLINFO_CONTENT_TYPE

The content type of the response.

4
3 CURLINFO_TOTAL_TIME

The total time of the last transfer.

4

CURL Error Constants

The curl_errno() and curl_error() are used with the following constants to handle errors:

Sr.No Constant & Description PHP Version
1 CURLE_OK

No error.

4
2 CURLE_UNSUPPORTED_PROTOCOL

The URL you passed to cURL used a protocol that this libcurl does not support.

4
3 CURLE_FAILED_INIT

Very early initialization code failed.

4
4 CURLE_URL_MALFORMAT

The URL was not properly formatted.

4
5 CURLE_COULDNT_RESOLVE_HOST

Couldn't resolve host. The given remote host was not resolved.

4

List of Functions

Here are some commonly used PHP cURL functions:

Sr.No Function & Description PHP Version
1 curl_close

Close a cURL session

4.0.2
2 curl_copy_handle

Copy a cURL handle along with all of its preferences

5
3 curl_errno

Return the last error number

4.0.3
4 curl_error

Return a string containing the last error for the current session

4.0.3
5 curl_escape

URL encodes the given string

5.5.0
6 curl_exec

Perform a cURL session

4.0.2
7 curl_getinfo

Get information regarding a specific transfer

4.0.4
8 curl_init

Initialize a cURL session

4.0.2
9 curl_multi_add_handle

Add a normal cURL handle to a cURL multi handle

5
10 curl_multi_close

Close a set of cURL handles

5
11 curl_multi_errno

Return the last multi curl error number

7.1.0
12 curl_multi_exec

Run the sub-connections of the current cURL handle

5
13 curl_multi_getcontent

Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set

5
14 curl_multi_info_read

Get information about the current transfers

5
15 curl_multi_init

Returns a new cURL multi handle

5
16 curl_multi_remove_handle

Remove a multi handle from a set of cURL handles

5
17 curl_multi_select

Wait for activity on any curl_multi connection

5
18 curl_multi_setopt

Set a cURL multi option

5.5.0
19 curl_multi_strerror

Return string describing error code

5.5.0
20 curl_pause

Pause and unpause a connection

5.5.0
21 curl_reset

Reset all options of a libcurl session handle

5.5.0
22 curl_setopt_array

Set multiple options for a cURL transfer

5.1.3
23 curl_setopt

Set an option for a cURL transfer

4.0.2
24 curl_share_close

Close a cURL share handle

5.5.0
25 curl_share_errno

Return the last share curl error number

7.1.0
26 curl_share_init

Initialize a cURL share handle

5.5.0
27 curl_share_setopt

Set an option for a cURL share handle

5.5.0
28 curl_share_strerror

Return string describing the given error code

7.1.0
29 curl_strerror

Return string describing the given error code

5.5.0
30 curl_unescape

Decodes the given URL encoded string

5.5.0
31 curl_upkeep

Performs any connection upkeep checks

8.2.0
32 curl_version

Gets cURL version information

4.0.2

Use Cases

These functions are required for PHP programs in order to manage responses and send requests to the web. They are very useful when interacting with APIs, downloading files, and scraping the web.

php_function_reference.htm
Advertisements