curl - 模拟浏览器

  • 作者:KK

  • 发表日期:2017.3.3


设置CURLOPT_USERAGENT即可

$url = 'http://' . $_SERVER['SERVER_NAME'] . '/b.php';

$curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_URL, $url);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);

$agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2141.400 QQBrowser/9.5.10219.400';
curl_setopt($curlHandler, CURLOPT_USERAGENT, $agent);

$result = curl_exec($curlHandler);
curl_close($curlHandler);

echo $result;
  • 测试:在相同目录下建一个“b.php”,代码是echo '收到的代理头是:' . $_SERVER['HTTP_USER_AGENT'];