php取不到curl来路的HTTP_ACCEPT_LANGUAGE
原因是在发起curl时未加HTTP_ACCEPT_LANGUAGE的header信息
添加
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-Language: zh-CN,zh;"))
可以加入更多的信息
一个较完整例子:
<?php
$url = "https://www.baidu.com/";
$ch = curl_init();
// 设置浏览器的特定header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Host: www.baidu.com",
"Connection: keep-alive",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Upgrade-Insecure-Requests: 1",
"DNT:1",
"Accept-Language: zh-CN,zh;",
'Cookie:_za=4540d427-eee1-435a-a533-66ecd8676d7d; "',
)
);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0');
// 在HTTP请求头中"Referer: "的内容。
curl_setopt($ch, CURLOPT_REFERER, "https://www.baidu.com/");
curl_setopt($ch, CURLOPT_ENCODING, "gzip, deflate, sdch");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //302redirect
// 针对https的设置
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$html = curl_exec($ch);
curl_close($ch);
if ($html === false) {
echo 'Curl error: ' . curl_error($ch) . "<br>\n\r";
} else {
echo $html;
}
?>
参考:
https://curl.haxx.se/libcurl/c/CURLOPT_HTTPHEADER.html
https://curl.haxx.se/libcurl/c/CURLOPT_HEADER.html