php PHP函数:CURL抓取网站内容的,支持301 302跳转

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php PHP函数:CURL抓取网站内容的,支持301 302跳转相关的知识,希望对你有一定的参考价值。

$curl_loops = 0;//避免死了循环必备
$curl_max_loops = 3;
 
function curl_get_file_contents($url, $referer='') {
global $curl_loops, $curl_max_loops;
$useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
if ($curl_loops++ >= $curl_max_loops) {
  $curl_loops = 0;
  return false;
} 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);?curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$data = curl_exec($ch);
$ret = $data;
list($header, $data) = explode("\r\n\r\n", $data, 2);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
if ($http_code == 301 || $http_code == 302) {
  $matches = array();
  preg_match('/Location:(.*?)\n/', $header, $matches);
  $url = @parse_url(trim(array_pop($matches)));
  if (!$url) {
  ?$curl_loops = 0;
  ?return $data;
  } 
  $new_url = $url['scheme'] . '://' . $url['host'] . $url['path']
   . (isset($url['query']) ? '?' . $url['query'] : '');
  $new_url = stripslashes($new_url);
  return curl_get_file_contents($new_url, $last_url);
} else {
  $curl_loops = 0;
  list($header, $data) = explode("\r\n\r\n", $ret, 2);
  return $data;
} 
}

以上是关于php PHP函数:CURL抓取网站内容的,支持301 302跳转的主要内容,如果未能解决你的问题,请参考以下文章

curl抓取网页内容php

PHP cURL库函数抓取页面内容

PHP通过CURL模拟登录并获取数据

PHP 抓取函数curl 实践

使用PHP curl模拟浏览器抓取网站信息

使用PHP的cURL库进行网页抓取