Comprehensive Guide to PHP cURL Functions: Usage, Parameters, and Practical Examples

This article provides an in-depth overview of PHP's cURL library, detailing its core functions, common options such as CURLOPT_URL and CURLOPT_POST, and practical code examples for GET, POST, custom headers, SSL handling, timeouts, file saving, and proxy usage.

php Courses
php Courses
php Courses
Comprehensive Guide to PHP cURL Functions: Usage, Parameters, and Practical Examples

cURL (Client URL) is a powerful open-source library for making network requests in PHP. It supports many protocols such as HTTP, HTTPS, FTP, and SMTP. This article explains the various parameters, functions, and uses of the cURL functions and demonstrates practical examples.

Basic Usage of cURL Functions

The cURL function is mainly used to send HTTP requests and receive server responses. The basic steps are:

Create a cURL resource handle: $ch = curl_init();

Set cURL options: curl_setopt($ch, option, value);

Execute the request and get the response: $response = curl_exec($ch);

Close the cURL resource handle: curl_close($ch);

Explanation of cURL Function Parameters and Uses

cURL provides many options; common ones include:

CURLOPT_URL : The URL to request.

CURLOPT_RETURNTRANSFER : When true, returns the response as a string instead of outputting it.

CURLOPT_POST : When true, sends a POST request.

CURLOPT_POSTFIELDS : Data to send in a POST request.

CURLOPT_HTTPHEADER : Adds custom HTTP request headers.

CURLOPT_SSL_VERIFYPEER : When false, disables SSL certificate verification.

CURLOPT_CONNECTTIMEOUT : Connection timeout in seconds.

CURLOPT_TIMEOUT : Overall request timeout in seconds.

CURLOPT_COOKIE : Adds cookie values.

CURLOPT_USERAGENT : Sets the User-Agent string.

CURLOPT_PROXY : Sets a proxy server address.

CURLOPT_FILE : Specifies a file path to save the response.

Functions and Uses of cURL

Sending a GET request: set CURLOPT_HTTPGET to true and specify the URL. Example:

$ch = curl_init();<br/>curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>$response = curl_exec($ch);<br/>curl_close($ch);<br/>echo $response;

Sending a POST request: set CURLOPT_POST to true and provide CURLOPT_POSTFIELDS. Example:

$ch = curl_init();<br/>curl_setopt($ch, CURLOPT_URL, "https://api.example.com/submit");<br/>curl_setopt($ch, CURLOPT_POST, true);<br/>curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John&[email protected]");<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>$response = curl_exec($ch);<br/>curl_close($ch);<br/>echo $response;

Adding custom HTTP headers: set CURLOPT_HTTPHEADER. Example:

$ch = curl_init();<br/>curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");<br/>curl_setopt($ch, CURLOPT_HTTPHEADER, array(<br/>    'Content-Type: application/json',<br/>    'Authorization: Bearer token'<br/>));<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>$response = curl_exec($ch);<br/>curl_close($ch);<br/>echo $response;

Handling SSL certificates: set CURLOPT_SSL_VERIFYPEER to false to disable verification.

Setting timeouts: use CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT to define connection and request time limits.

Saving response to a file: set CURLOPT_FILE with a file handle. Example:

$ch = curl_init();<br/>curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>curl_setopt($ch, CURLOPT_FILE, fopen("response.txt", "w"));<br/>curl_exec($ch);<br/>curl_close($ch);

Using a proxy server: set CURLOPT_PROXY with the proxy address. Example:

$ch = curl_init();<br/>curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");<br/>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>curl_setopt($ch, CURLOPT_PROXY, "http://proxy.example.com:8080");<br/>$response = curl_exec($ch);<br/>curl_close($ch);<br/>echo $response;

Conclusion

The cURL function is a versatile tool for sending HTTP requests and retrieving server responses. By configuring various options, developers can meet diverse request requirements. This article has thoroughly analyzed cURL parameters, functions, and use cases, providing practical examples to aid developers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HTTPPHPAPINetworkingcURL
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.