In PHP, the cURL library can be used to simulate the sending of HTTP requests and configure request headers, thereby forging headers for web page scraping. Here is an example code:
// 创建 cURL 句柄
$ch = curl_init();
// 设置请求 URL
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com');
// 设置请求头信息
$headers = array(
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Referer: https://www.google.com/',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 设置其他 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 执行 HTTP 请求
$response = curl_exec($ch);
// 检查是否有错误发生
if(curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
}
// 关闭 cURL 句柄
curl_close($ch);
// 输出响应结果
echo $response;In the above code, we first create a cURL handle and set the requested URL as well as the request headers. The request headers include common fields such as User-Agent, Accept, Accept-Language, and Referer; you can add or modify these fields as needed.
Additionally, we have configured other cURL options—such as CURLOPT_RETURNTRANSFER, CURLOPT_FOLLOWLOCATION, CURLOPT_SSL_VERIFYHOST, and CURLOPT_SSL_VERIFYPEER—which allow you to control cURL's behavior, for example, whether to return the response, whether to follow redirects, or whether to verify SSL certificates, among other things.
Finally, we send an HTTP request and display the response on the page. If an error occurs, we also output the cURL error message using the `curl_errno` and `curl_error` functions.