爬蟲、測試、API 對接常需偽造客戶端 IP 與 Referer。本文匯總 cURL、fsockopen、Stream Context 三種底層實作方式,並詳細列出 cURL 關鍵選項對照表與代理、SSL、認證等進階場景。
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/search.asp?page=3&keyword=test');
curl_setopt($ch, CURLOPT_REFERER, 'https://example.com/'); // 偽造 Referer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'CLIENT-IP: 1.2.3.4', // 偽造 IP(需對方支援)
'X-FORWARDED-FOR: 1.2.3.4',
]);
$result = curl_exec($ch);
curl_close($ch);
?>
<?php
$host = 'example.com';
$target = '/test.asp';
$referer = 'https://example.com/';
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$out = "GET $target HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Referer: $referer\r\n";
$out .= "X-FORWARDED-FOR: 1.2.3.4\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) echo fgets($fp, 1024);
fclose($fp);
}
?>
<?php
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"Referer: https://example.com/\r\n" .
"X-FORWARDED-FOR: 1.2.3.4\r\n",
'content' => http_build_query(['key' => 'value']),
]
]);
$result = file_get_contents('https://example.com/api', false, $context);
?>
| 選項 | 用途 | 常用值 |
|---|---|---|
| CURLOPT_URL | 請求網址 | 字串 |
| CURLOPT_REFERER | 偽造 Referer | 字串 |
| CURLOPT_USERAGENT | 偽造 User-Agent | 字串 |
| CURLOPT_HTTPHEADER | 自訂請求標頭 | 陣列 |
| CURLOPT_RETURNTRANSFER | 回傳字串而非直接輸出 | 1 |
| CURLOPT_POST | 啟用 POST | 1 |
| CURLOPT_POSTFIELDS | POST 資料 | 字串/陣列 |
| CURLOPT_HEADER | 包含回應標頭 | 1 |
| CURLOPT_FOLLOWLOCATION | 自動追蹤重導向 | 1 |
| CURLOPT_TIMEOUT | 超時秒數 | 整數 |
| CURLOPT_PROXY | 代理伺服器 | ip:port |
| CURLOPT_PROXYUSERPWD | 代理認證 | user:pass |
| CURLOPT_SSL_VERIFYPEER | 驗證 SSL 憑證 | 0(關閉)、1(開啟) |
| CURLOPT_COOKIEJAR | 儲存 Cookie 檔案 | 路徑 |
| CURLOPT_COOKIEFILE | 讀取 Cookie 檔案 | 路徑 |
<?php
$fields = ['lname'=>'Wang', 'fname'=>'Ming', 'email'=>'test@example.com'];
$fields_string = http_build_query($fields);
$ch = curl_init('https://example.com/submit.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
?>
<?php
$ch = curl_init('https://api.example.com/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, 'proxy.example.com:8080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 測試環境關閉驗證
$result = curl_exec($ch);
curl_close($ch);
?>
<?php
function curlRequest($url, $postFields = '', $proxy = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; PHP Bot)');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept-Language: zh-CN',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
]);
if ($postFields) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if ($proxy) curl_setopt($ch, CURLOPT_PROXY, $proxy);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 405) die("Bad proxy: $proxy");
return $result;
}
?>
X-FORWARDED-FOR 真實性,單純偽造標頭效果有限robots.txt