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

来源:互联网 发布:0桩号的右侧无数据 编辑:程序博客网 时间:2024/04/29 05:10

我们在抓取网站内容的时候,经常遇到稀奇古怪的防盗链,比如上次碰到一个站的图片地址是假的,访问后要301跳转一次才到真正的图片路径,这个真实的路径又做了防盗措施,判断referer是不是上个假的图片地址。用curl试了几次,终于整出一个函数,效果不错。


$curl_loops = 0;//避免死了循环必备02$curl_max_loops = 3;03  04function curl_get_file_contents($url, $referer='') {05global $curl_loops, $curl_max_loops;06$useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";07if ($curl_loops++ >= $curl_max_loops) {08  $curl_loops = 0;09  return false;10}11$ch = curl_init();12curl_setopt($ch, CURLOPT_URL, $url);?curl_setopt($ch, CURLOPT_HEADER, true);13curl_setopt($ch, CURLOPT_USERAGENT, $useragent);14curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);15curl_setopt($ch, CURLOPT_REFERER, $referer);16$data = curl_exec($ch);17$ret = $data;18list($header, $data) = explode("\r\n\r\n", $data, 2);19$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);20$last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);21curl_close($ch);22if ($http_code == 301 || $http_code == 302) {23  $matches = array();24  preg_match('/Location:(.*?)\n/', $header, $matches);25  $url = @parse_url(trim(array_pop($matches)));26  if (!$url) {27  ?$curl_loops = 0;28  ?return $data;29  }30  $new_url = $url['scheme'] . '://' . $url['host'] . $url['path']31   . (isset($url['query']) ? '?' . $url['query'] : '');32  $new_url = stripslashes($new_url);33  return curl_get_file_contents($new_url, $last_url);34} else {35  $curl_loops = 0;36  list($header, $data) = explode("\r\n\r\n", $ret, 2);37  return $data;38}39}


原创粉丝点击