实例介绍
<?php
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 的 <head> 区段
// <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
// <meta content="text/html; charset=gb2312" http-equiv="Content-Type">
preg_match("/<head.*>(.*)<\/head>/smUi",$html, $htmlHeaders);
//var_dump($output);die();
if(!count($htmlHeaders)){
$title = "无法解析数据中的 <head> 区段";
}
// 取得 <head> 中 meta 设置的编码格式<meta charset="gb2312">
if(preg_match('/<meta.*charset=(("){0,1}[a-zA-Z0-9-]*("){0,1})/',$htmlHeaders[1], $results)){
$charset = $results[1];
}else{
$charset = "None";
}
$charset = str_replace('"','',$charset);
//取得 <title> 中的文字
if(preg_match("/<title>(.*)<\/title>/Ui",$htmlHeaders[1], $htmlTitles)){
if(!count($htmlTitles)){
$title = "无法解析 <title> 的内容";
exit;
}
// 将<title> 的文字编码格式转成 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 '</br>';
}
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 的 <head> 区段
// <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
// <meta content="text/html; charset=gb2312" http-equiv="Content-Type">
preg_match("/<head.*>(.*)<\/head>/smUi",$html, $htmlHeaders);
//var_dump($output);die();
if(!count($htmlHeaders)){
$title = "无法解析数据中的 <head> 区段";
}
// 取得 <head> 中 meta 设置的编码格式<meta charset="gb2312">
if(preg_match('/<meta.*charset=(("){0,1}[a-zA-Z0-9-]*("){0,1})/',$htmlHeaders[1], $results)){
$charset = $results[1];
}else{
$charset = "None";
}
$charset = str_replace('"','',$charset);
//取得 <title> 中的文字
if(preg_match("/<title>(.*)<\/title>/Ui",$htmlHeaders[1], $htmlTitles)){
if(!count($htmlTitles)){
$title = "无法解析 <title> 的内容";
exit;
}
// 将<title> 的文字编码格式转成 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 '</br>';
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论