php采集网页标题图片,PHP获取网页title标题

function curl_get_file_contents($url,$referer='') {

set_time_limit(0);

static $curl_loops = 0;//避免死了循环必备

static $curl_max_loops = 3;

$useragent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);

curl_setopt($ch,CURLOPT_HEADER,true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书

curl_setopt($ch,CURLOPT_USERAGENT,$useragent);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

curl_setopt($ch,CURLOPT_REFERER,$referer);

curl_setopt($ch, CURLOPT_TIMEOUT,60);

$data = curl_exec($ch);

$ret = $data;

list($header,$data) = explode("\r\n\r\n",$data,2);

$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

$last_url = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);

curl_close($ch);

if ($http_code == 301 || $http_code == 302) {

$matches = array();

preg_match('/Location:(.*?)\n/',$header,$matches);

$url = @parse_url(trim(array_pop($matches)));

if (!$url) {

return $data;

}

$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query']) ? '?' . $url['query'] : '');

if ($curl_loops >= $curl_max_loops) {

return false;

}else {

$new_url = stripslashes($new_url);

return curl_get_file_contents($new_url);

}

} else {

list($header,$data) = explode("\r\n\r\n",$ret,2);

return $data;

}

}

function get_title_contents($html){

// 解析 HTML 的

区段

// 

// 

preg_match("/

(.*)/smUi",$html, $htmlHeaders);

//var_dump($output);die();

if(!count($htmlHeaders)){

$title = "无法解析数据中的

区段";

}

// 取得

中 meta 设置的编码格式

if(preg_match('/

$charset =  $results[1];

}else{

$charset = "None";

}

$charset = str_replace('"','',$charset);

//取得

中的文字

if(preg_match("/

(.*)/Ui",$htmlHeaders[1], $htmlTitles)){

if(!count($htmlTitles)){

$title = "无法解析

的内容";

exit;

}

// 将

的文字编码格式转成 UTF-8

if($charset == "None"){

$title=$htmlTitles[1];

}else{

$title=iconv($charset, "UTF-8", $htmlTitles[1]);

}

}

return html_entity_decode($title);

}

/*网址写入*/

$urla = array('这里填入数组网址'

);

foreach ($urla as $key => $url) {

$html = curl_get_file_contents($url);

$title = get_title_contents($html);

echo $title;

echo '';

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值