cURL 是 PHP 中最強大的網絡請求工具,支持 HTTP/HTTPS、FTP、SFTP 等多種協議,提供 SSL 證書、Cookie、代理、斷點續傳等豐富功能。本文通過實戰案例,系統講解 cURL 的 POST 請求、XML/JSON 數據提交、飛信/短信接口調用,以及高性能多線程併發實現。
php.ini,取消註釋 extension=php_curl.dlllibeay32.dll、ssleay32.dll、php_curl.dll 複製到系統目錄 C:\Windows\System32# Ubuntu/Debian
sudo apt-get install php-curl
sudo systemctl restart apache2
# CentOS/RHEL
sudo yum install php-curl
sudo systemctl restart httpd
<?php
// 1. 初始化
$ch = curl_init();
// 2. 設置選項
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.example.com/endpoint',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(['key' => 'value']),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false, // 僅測試環境使用
]);
// 3. 執行請求
$response = curl_exec($ch);
// 4. 錯誤處理
if (curl_errno($ch)) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
// 5. 關閉資源
curl_close($ch);
// 6. 處理響應
$data = json_decode($response, true);
?>
<?php
$xml_data = '<?xml version="1.0" encoding="UTF-8"?>
<request>
<message>測試內容</message>
</request>';
$url = 'https://api.example.com/submit';
$headers = [
'Content-Type: text/xml; charset=UTF-8',
'Content-Length: ' . strlen($xml_data)
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $xml_data,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
error_log("cURL Error: " . curl_error($ch));
}
curl_close($ch);
echo $response;
?>
<?php
$postData = [
'username' => '13800138000',
'password' => '123456',
'sendto' => '13912345678',
'message' => '測試短信內容!'
];
$ch = curl_init('https://sms.api.example.com/send');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 15,
]);
$result = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
傳統串行請求耗時 = 所有請求耗時之和。多線程併發可將總耗時降為最慢單個請求的耗時,對聚合多個 API、爬蟲、批量推送場景極其關鍵。
<?php
class CurlMulti {
public static function request(array $urls, int $timeout = 10): array {
$handles = [];
$mh = curl_multi_init();
$results = [];
// 初始化所有 handle
foreach ($urls as $key => $url) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 5,
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; PHP CurlMulti)',
CURLOPT_HEADER => false,
]);
$handles[$key] = $ch;
curl_multi_add_handle($mh, $ch);
}
// 執行併發請求
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc === CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc === CURLM_OK) {
if (curl_multi_select($mh) !== -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc === CURLM_CALL_MULTI_PERFORM);
}
}
// 收集結果
foreach ($handles as $key => $ch) {
$error = curl_error($ch);
$results[$key] = [
'content' => curl_multi_getcontent($ch),
'info' => curl_getinfo($ch),
'error' => $error ?: null,
];
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
curl_multi_close($mh);
return $results;
}
}
// 使用示例
$urls = [
'https://api.github.com',
'https://httpbin.org/get',
'https://jsonplaceholder.typicode.com/posts/1',
];
$results = CurlMulti::request($urls, 10);
print_r($results);
?>
curl_multi_execCURLOPT_TIMEOUT 控制單請求上限,防止阻塞| 場景 | 建議設置 |
|---|---|
| 第三方 API 調用 | 設置合理超時、啟用 SSL 驗證、記錄請求日志 |
| 高併發聚合 | 使用 curl_multi、連接池複用、限流熔斷 |
| 文件上傳 | 使用 CURLFile 類、設置 CURLOPT_INFILESIZE |
| 長連接複用 | 設置 CURLOPT_FORBID_REUSE => false、CURLOPT_TCP_KEEPALIVE => true |
new CURLFile($path)CURLOPT_CAINFO 指定 CA 證書包,勿長期關閉驗證CURLOPT_WRITEFUNCTION 流式寫入磁盤,避免內存溢出urlencode,接收後 urldecode掌握 cURL 從基礎到多線程併發,是 PHP 開發者處理外部服務集成、微服務通信、數據採集的核心技能。建議在項目中封裝統一的 HTTP 客戶端類,統一異常處理、重試策略、監控指標,提升系統穩定性。