用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1630|回复: 0
收起左侧

[语言 Language] php curl根据网址获取标题

[复制链接]
发表于 2016-5-12 13:59:27 | 显示全部楼层 |阅读模式
  1. <?php
  2. /****/
  3. //Gary xu
  4. //1122557724@qq.com
  5. /****/
  6. namespace Xuyaoxiang;

  7.     class Snoopy {
  8.      
  9.     public $pattern_array=array(
  10.     'title'=>'/<title>(\s*.*)<\/title>/i',
  11.     'description'=>'/<meta +name="[d|D]escription" +content="(.*)" +\/>/',
  12.     'charset'=>'/charset="?([\w-]+)"?/i',
  13.     );
  14.      
  15.     public $user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'; //模拟浏览器头部数据
  16.      
  17.     public $target_code="utf-8"; //目标编码
  18.      
  19.     public $url;
  20.      
  21.     public $data;
  22.      
  23.     public $pattern_key;
  24.      
  25.     function __construct($url)
  26.     {
  27.             $this->url=$url;
  28.     }
  29.      
  30.     public function set_pattern($key,$val)
  31.     {
  32.         $this->pattern_array[$key]=$val;
  33.     }
  34.      
  35.      
  36.      
  37.     function get_content($pattern_key)
  38.     {
  39.         $this->pattern_key=$pattern_key;
  40.          
  41.         if($this->pattern_key==''){return false;}
  42.          
  43.         $this->curl_get_data();
  44.          
  45.         if($this->data==false){return false;}
  46.          
  47.         $charset=$this->get_charset();
  48.          
  49.         $this->check_charset($charset);
  50.          
  51.         $content=$this->get_key_content();
  52.          
  53.         return  trim($content[1]);
  54.     }
  55.      
  56.      
  57.             function curl_get_data()
  58.         {
  59.                 $curl=curl_init();
  60.                 // 设置你需要抓取的URL
  61.             
  62.                 curl_setopt($curl, CURLOPT_URL, $this->url);
  63.          
  64.                 // 设置header
  65.                 curl_setopt($curl, CURLOPT_HEADER, 0);
  66.                  
  67.                 // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
  68.                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  69.                  
  70.                 curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
  71.             
  72.                 // 运行cURL,请求网页
  73.                  
  74.                 $this->data = curl_exec($curl);
  75.                  
  76.                 curl_close($curl);
  77.         }
  78.          
  79.         function check_charset($page_charset)
  80.         {
  81.              if($page_charset!=$this->target_code)
  82.              {
  83.                 $this->data=mb_convert_encoding($this->data,$this->target_code,$page_charset);
  84.              }
  85.         }
  86.          
  87.         function get_key_content()
  88.         {
  89.             preg_match($this->pattern_array[$this->pattern_key],$this->data,$content);
  90.             return $content;   
  91.         }
  92.          
  93.         function get_charset()
  94.         {
  95.             preg_match($this->pattern_array['charset'],$this->data,$reg_charset);
  96.             return $page_charset=strtolower($reg_charset[1]);  
  97.         }
  98. }


  99. header("Content-type:text/html;charset=utf-8");
  100.      $snoopy=new snoopy("http://www.qq.com");
  101.      
  102.      $title=$snoopy->get_content('title');
  103.          
  104.      print_r($title);
  105. ?>
复制代码

本版积分规则