cURL是PHP最強大的HTTP客戶端庫,支持HTTP/HTTPS、FTP、代理、認證、並發等豐富特性。本文從基礎GET/POST到生產級封裝,涵蓋超時、重試、並發、證書驗證等實戰技巧。
<?php
$ch = curl_init('https://api.example.com/users/1');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, // 返回字符串而非直接輸出
CURLOPT_FOLLOWLOCATION => true, // 跟隨重定向
CURLOPT_MAXREDIRS => 5,
CURLOPT_TIMEOUT => 10, // 總超時秒數
CURLOPT_CONNECTTIMEOUT => 3, // 連接超時
CURLOPT_SSL_VERIFYPEER => true, // 驗證SSL證書(生產必開)
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_USERAGENT => 'MyApp/1.0 (+https://example.com/bot)',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException('cURL錯誤: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($response, true);
$postData = ['name' => 'test', 'email' => 'test@example.com'];
$ch = curl_init('https://api.example.com/users');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($postData), // application/x-www-form-urlencoded
// 文件上傳 multipart/form-data
// CURLOPT_POSTFIELDS => ['file' => new CURLFile('/path/to/file.png', 'image/png', 'avatar.png')],
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
CURLOPT_RETURNTRANSFER => true,
]);
class HttpClient {
private $defaultOptions = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 3,
CURLOPT_TIMEOUT => 15,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_ENCODING => 'gzip,deflate', // 自動解壓
];
public function request(string $method, string $url, array $data = [], array $headers = []): array {
$ch = curl_init($url);
$opts = $this->defaultOptions;
$opts[CURLOPT_CUSTOMREQUEST] = strtoupper($method);
if ($data) {
$opts[CURLOPT_POSTFIELDS] = is_array($data) ? http_build_query($data) : $data;
}
if ($headers) {
$opts[CURLOPT_HTTPHEADER] = array_map(fn($k,$v) => "$k: $v", array_keys($headers), $headers);
}
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($errno) throw new RuntimeException("cURL $errno: $error");
return ['body' => $response, 'code' => $info['http_code'], 'headers' => $info];
}
public function get(string $url, array $headers = []): array { return $this->request('GET', $url, [], $headers); }
public function post(string $url, array $data, array $headers = []): array { return $this->request('POST', $url, $data, $headers); }
}
function multi_request(array $urls): array {
$mh = curl_multi_init();
$handles = [];
foreach ($urls as $i => $url) {
$ch = curl_init($url);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10]);
curl_multi_add_handle($mh, $ch);
$handles[$i] = $ch;
}
$running = null;
do { curl_multi_exec($mh, $running); } while ($running > 0);
$results = [];
foreach ($handles as $i => $ch) {
$results[$i] = ['body' => curl_multi_getcontent($ch), 'info' => curl_getinfo($ch)];
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
return $results;
}
$ch復用(重置CURLOPT_URL),利用Keep-Alive。CURLOPT_CAINFO指向最新cacert.pem(定期更新)。Authorization、Cookie等頭部。掌握cURL細節,能構建穩健、高性能的外部服務集成層。