>  기사  >  php教程  >  php curl通过连接获取标题

php curl通过连接获取标题

WBOY
WBOY원래의
2016-06-07 11:34:261336검색

php curl通过连接获取标题
/****/<br> //Gary xu<br> //1122557724@qq.com<br> /****/<br> namespace Xuyaoxiang;<br>  <br>     class Snoopy {<br>      <br>     public $pattern_array=array(<br>     'title'=>'/<title>(\s*.*)/i',<br>     'description'=>'/<meta>/',<br>     'charset'=>'/charset=\"?([\w-]+)\"?/i',<br>     );<br>      <br>     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'; //模拟浏览器头部数据<br>      <br>     public $target_code="utf-8"; //目标编码<br>      <br>     public $url;<br>      <br>     public $data;<br>      <br>     public $pattern_key;<br>      <br>     function __construct($url)<br>     {<br>             $this->url=$url;<br>     }<br>      <br>     public function set_pattern($key,$val)<br>     {<br>         $this->pattern_array[$key]=$val;<br>     }<br>      <br>      <br>      <br>     function get_content($pattern_key)<br>     {<br>         $this->pattern_key=$pattern_key;<br>          <br>         if($this->pattern_key==''){return false;}<br>          <br>         $this->curl_get_data();<br>          <br>         if($this->data==false){return false;}<br>          <br>         $charset=$this->get_charset();<br>          <br>         $this->check_charset($charset);<br>          <br>         $content=$this->get_key_content();<br>          <br>         return  trim($content[1]);<br>     }<br>      <br>      <br>             function curl_get_data()<br>         {<br>                 $curl=curl_init();<br>                 // 设置你需要抓取的URL<br>              <br>                 curl_setopt($curl, CURLOPT_URL, $this->url);<br>          <br>                 // 设置header<br>                 curl_setopt($curl, CURLOPT_HEADER, 0);<br>                  <br>                 // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。<br>                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<br>                  <br>                 curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);<br>              <br>                 // 运行cURL,请求网页<br>                  <br>                 $this->data = curl_exec($curl); <br>                  <br>                 curl_close($curl);<br>         }<br>          <br>         function check_charset($page_charset)<br>         {<br>              if($page_charset!=$this->target_code)<br>              {<br>                 $this->data=mb_convert_encoding($this->data,$this->target_code,$page_charset);<br>              }<br>         }<br>          <br>         function get_key_content()<br>         {<br>             preg_match($this->pattern_array[$this->pattern_key],$this->data,$content);<br>             return $content;   <br>         }<br>          <br>         function get_charset()<br>         {<br>             preg_match($this->pattern_array['charset'],$this->data,$reg_charset);<br>             return $page_charset=strtolower($reg_charset[1]);  <br>         }<br> }<br>  <br>  <br> header("Content-type:text/html;charset=utf-8");<br>      $snoopy=new snoopy("http://www.qq.com");<br>      <br>      $title=$snoopy->get_content('title');<br>          <br>      print_r($title);</title>

AD:真正免费,域名+虚机+企业邮箱=0元

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.