Mastering PHP cURL: From Basics to Advanced Parallel Requests

This guide explains PHP’s cURL extension, covering installation, core concepts, step‑by‑step request setup, and practical examples for GET, POST, file download, redirect handling, error logging, and parallel requests, helping backend developers efficiently interact with external APIs and services.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Mastering PHP cURL: From Basics to Advanced Parallel Requests

Overview

cURL is both a PHP library and a command‑line tool that enables data transfer using URL syntax. It supports protocols such as HTTP, HTTPS, FTP, and more, providing features for SSL, cookies, and authentication.

What Is PHP cURL?

PHP cURL wraps the libcurl library, allowing PHP scripts to act as URL clients. It can be used via the libcurl library (embedded in applications) or the cURL command‑line tool.

cURL supports protocols including HTTP, HTTPS, FTP, FTPS, GOPHER, TELNET, DICT, and FILE.

Installing the PHP cURL Extension

Open the php.ini configuration file.

Locate the line extension=php_curl.dll.

Remove the leading semicolon ( ;) to enable the extension.

Restart the PHP‑FPM service (not required for CLI).

Basic Request Cycle

Step 1 – Initialize a cURL Session

$curl = curl_init();

Step 2 – Set Session Options

curl_setopt($handle, CURLOPT_URL, $url);

Step 3 – Execute the Request

$response = curl_exec($handle);

Step 4 – Close the Session

curl_close($handle);

Simple GET Example

<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.example.com");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if ($response === false) {
    echo "cURL Error: " . curl_error($curl);
} else {
    echo $response;
}
curl_close($curl);

GET Request with PHP cURL

<?php
$url = "https://api.example.com/endpoint?key1=value1&key2=value2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

POST Request with JSON Payload

<?php
$url = "https://api.example.com/endpoint";
$data = ["key1" => "value1", "key2" => "value2"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Downloading a File

<?php
$url = "https://www.example.com/file.zip";
$file = "file.zip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
$fp = fopen($file, "w");
fwrite($fp, $data);
fclose($fp);

Handling Redirects (HTTP 301/302)

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

Logging cURL Errors to a File

<?php
$url = "https://api.example.com/endpoint";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
    $error_message = curl_error($ch);
    $log_file = "curl_error.log";
    $log = fopen($log_file, "a");
    fwrite($log, "[" . date("Y-m-d H:i:s") . "] " . $error_message . "
");
    fclose($log);
}
curl_close($ch);
echo $response;

Parallel Multiple cURL Requests

<?php
$mh = curl_multi_init();
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL, "http://example.com/api1");
curl_multi_add_handle($mh, $curl1);
$curl2 = curl_init();
curl_setopt($curl2, CURLOPT_URL, "http://example.com/api2");
curl_multi_add_handle($mh, $curl2);
$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running);
$result1 = curl_multi_getcontent($curl1);
$result2 = curl_multi_getcontent($curl2);
curl_multi_remove_handle($mh, $curl1);
curl_multi_remove_handle($mh, $curl2);
curl_multi_close($mh);

Common Uses of PHP cURL

Send HTTP requests (GET, POST, PUT, DELETE) to APIs or web services.

Web scraping to extract data from HTML pages.

Download files such as images or documents.

Upload files via POST or PUT.

Check URL availability and status codes.

Transfer data between servers for backend processing.

Use various protocols (HTTP, HTTPS, FTP, FTPS, etc.).

Conclusion

cURL, together with the libcurl library, provides a powerful, cross‑platform way for PHP developers to automate data retrieval, interact with APIs, handle redirects, log errors, and perform parallel requests, making it an essential tool for modern backend development.

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.

HTTPPHPAPIcURLlibcurlparallel requests
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

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.