cURL 是 PHP 與外部 HTTP 服務互動的核心工具。本文封裝兩個高頻場景——模擬表單登入(含 Cookie 管理)與 SOAP XML 請求——為可直接復用的函數,並詳細註解關鍵參數。
vcurl()支援 GET/POST 自動判斷、Cookie 讀寫、Referer 自動/手動、User-Agent 偽裝、302 跳轉追蹤、超時控制、錯誤捕獲。
<?php
/**
* 通用 cURL 請求封裝(模擬登入專用)
* @param string $url 請求網址
* @param array|string $post POST 資料,空則為 GET
* @param string $cookie 手動攜帶 Cookie 字串
* @param string $cookiejar Cookie 檔案路徑(自動讀寫)
* @param string $referer 來源頁面
* @return string|false 回應內容或錯誤訊息
*/
function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = '') {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
// 偽裝瀏覽器 User-Agent
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0 (compatible; PHP cURL)');
// Referer 處理
if ($referer) {
curl_setopt($curl, CURLOPT_REFERER, $referer);
} else {
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
}
// POST 請求處理
if ($post) {
curl_setopt($curl, CURLOPT_POST, true);
// 若 $post 是陣列,自動編碼為 application/x-www-form-urlencoded
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
// Cookie 處理
if ($cookie) curl_setopt($curl, CURLOPT_COOKIE, $cookie);
if ($cookiejar) {
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiejar); // 寫入
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiejar); // 讀取
}
// 通用選項
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // 支援 302 跳轉
curl_setopt($curl, CURLOPT_TIMEOUT, 100); // 最大執行秒數
curl_setopt($curl, CURLOPT_HEADER, false); // 不包含回應標頭
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 回傳字串而非直接輸出
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 忽略 SSL 憑證驗證(測試用,生產建議開啟)
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($curl);
if (curl_errno($curl)) {
$response = '<pre><b>錯誤:</b><br />' . curl_error($curl) . '</pre>';
}
curl_close($curl);
return $response;
}
?>
<?php
$loginUrl = 'https://example.com/login.php';
$postData = ['username' => 'test', 'password' => '123456'];
$cookieFile = '/tmp/cookie_' . md5($loginUrl) . '.txt';
// 1. 發送登入 POST,自動儲存 Cookie 到檔案
$loginResult = vcurl($loginUrl, $postData, '', $cookieFile);
echo "登入結果:$loginResult\n";
// 2. 攜帶 Cookie 訪問會員中心
$memberUrl = 'https://example.com/member.php';
$memberPage = vcurl($memberUrl, '', '', $cookieFile);
echo "會員頁面內容長度:" . strlen($memberPage);
?>
vcurlsoap()專為 SOAP 1.1/1.2 設計,自動組裝 Content-Type: text/xml、SOAPAction 標頭,支援 HTTPS。
<?php
/**
* 發送 SOAP 請求
* @param string $url SOAP 端點網址
* @param string $SoapRequest 完整 SOAP XML 內容
* @param string $SoapAction SOAPAction 標頭值
* @return string 伺服器 XML 回應
*/
function vcurlsoap($url, $SoapRequest, $SoapAction) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $SoapRequest);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 生產環境請設 true 並配置 CA 憑證
$headers = [
"Content-Type: text/xml; charset=utf-8",
"Content-Length: " . strlen($SoapRequest),
];
if (!is_null($SoapAction)) {
$headers[] = 'SOAPAction: "' . $SoapAction . '"';
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$response = 'cURL Error: ' . curl_error($ch);
}
curl_close($ch);
return $response;
}
?>
<?php
$wsdlUrl = 'https://www.webxml.com.cn/WebServices/WeatherWebService.asmx';
$soapAction = 'https://WebXml.com.cn/getWeatherbyCityName';
$xmlRequest = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
<getWeatherbyCityName xmlns="https://WebXml.com.cn/">
<cityName>北京</cityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>';
$response = vcurlsoap($wsdlUrl, $xmlRequest, $soapAction);
echo $response; // 解析 XML 取得天氣資訊
?>
CURLOPT_COOKIEJAR/FILE 指定檔案路徑,跨請求自動維持 Session。CURLOPT_CAINFO 指向 cacert.pem,開啟 CURLOPT_SSL_VERIFYPEER。curl_errno() 並記錄 curl_error(),便於除錯。CURLOPT_TCP_KEEPALIVE;大量並發請求建議使用 curl_multi_* 系列函數。這兩個封裝函數涵蓋了 90% 的 cURL 業務場景:vcurl() 適合一般 REST API、表單提交、爬蟲登入;vcurlsoap() 專攻企業級 SOAP 整合。直接複製到專案工具類即可使用。