
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.