• 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; 
    } 
  • 相关阅读:
    toj 2975 Encription
    poj 1797 Heavy Transportation
    toj 2971 Rotating Numbers
    zoj 2281 Way to Freedom
    toj 2483 Nasty Hacks
    toj 2972 MOVING DHAKA
    toj 2696 Collecting Beepers
    toj 2970 Hackle Number
    toj 2485 Card Tric
    js页面定位,相关几个属性
  • 原文地址:https://www.cnblogs.com/chenjian/p/4428271.html
Copyright © 2020-2023  润新知