当前位置 : 主页 > 网络编程 > PHP >

curlGetTitle.php

来源:互联网 收集:自由互联 发布时间:2021-06-28
curlGetTitle.php * @return array('title'=title,'url'=url) 一个关联数组,包含title,url*/function curlGetTitle($url=""){if(!$url)return 'url为空';$curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($c
curlGetTitle.php
 
	* @return array('title'=>title,'url'=>url) 一个关联数组,包含title,url
	*/
	function curlGetTitle($url=""){
		if(!$url)return 'url为空';
		$curl = curl_init();
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_TIMEOUT, 500); 

	    preg_match("/^(http:\/\/|https:\/\/).*$/",$url,$match);
		if($match['1']!='https://' && $match[1]!='http://'){
			return '这不是一个链接';
		}
		//判断https加ssl验证 ,不验证ssl
		if($match[1]=='https://'){
			curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
	    curl_setopt($curl, CURLOPT_URL, $url);
	    $data = curl_exec($curl);
	    // curl_error();
	    curl_close($curl);  
		$pos = strpos($data,'utf-8');

		if($pos===false){$data = iconv("gbk","utf-8",$data);}
		preg_match("/(.*)<\/title>/i",$data, $title);
		if(empty($title) || !isset($title)){ // title为空
			return '该页面没有title!';
		}
		$res = array('title'=>$title[1],'url'=>$url);
		return $res;

	}
	$url = 'https://baidu.com';
	var_dump(curlGetTitle($url));</pre>
网友评论