zoukankan      html  css  js  c++  java
  • php 读取网站页面源码的经典函数

    Snoopy.class.php下载

    include "inc/Snoopy.class.php"; 
            //读取网页,返回网页源文件内容 
    function read_url($str){ 
                       $snoopy = new Snoopy;
                        $snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; 
                       //$snoopy->agent="Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36";
                       $snoopy->rawheaders["Pragma"] = "no-cache";
                       $snoopy->maxredirs = 2; 
                       $snoopy->offsiteok = false; 
                       $snoopy->expandlinks = false;
                    if($snoopy->fetch($str)) 
                       { 
                           $result=htmlspecialchars($snoopy->results); 
                       } 
                   
                 if ($result=="")
                 {
                 
                      if(function_exists("curl_init"))
                          {
                             if(!function_exists("lex_curl"))
                                 {
                                   function lex_curl($url)
                                     {
                                         $ch = curl_init();
                                         curl_setopt ($ch, CURLOPT_URL, $url);
                                         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
                                         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 30);
                                         curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
                                         curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                                         curl_setopt ($ch, CURLOPT_MAXREDIRS, 2);
                                         $file_contents = curl_exec($ch);
                                         curl_close($ch);
                                          if($file_contents=="1") $file_contents="";
                                          return $file_contents;
                                      }
                                  }
                               $lex_function_get="lex_curl";
                           }
                       else 
                          {$lex_function_get="file_get_contents";}
                  $result=@$lex_function_get($str) or die($str.'->远程网址读取失败!');
                
                 }
                 
        return $result; 
    } 
  • 相关阅读:
    BZOJ4036 HAOI2015按位或(概率期望+容斥原理)
    洛谷p2661信息传递题解
    洛谷P1434滑雪题解及记忆化搜索的基本步骤
    二分图最大匹配
    线段树

    图论基本算法
    并查集
    RMQ--ST表
    矩阵快速幂和矩阵乘法
  • 原文地址:https://www.cnblogs.com/chenjian/p/4428271.html
Copyright © 2011-2022 走看看