欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php curl通过连接获取标题

程序员文章站 2022-05-15 15:42:45
...
php curl通过连接获取标题
/****/
//Gary xu
//1122557724@qq.com
/****/
namespace Xuyaoxiang;

class Snoopy {

public $pattern_array=array(
'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);

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