资源简介
<?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>';
}
代码片段和文件信息
- 上一篇:upload-lab靶场PHP源码
- 下一篇:php获取企业微信用户信息
相关资源
- php获取企业微信用户信息
- upload-lab靶场PHP源码
- ucms_1.4.8 php源码
- 微信小程序支付付款Deom(php|小程序代
- PHP图片验证码案列
- 学生信息登记表(php代码)
- php上传、编辑用户头像
- PHP许愿墙(源码+数据库)
- php查询天气预报
- 公司核名系统(php源码)
-
php excel 操作源码 xm
l - php 日历控件(DATE PICKER - JQUERY PLUGIN)
- php 日历(bootstrap-datetimepicker)
-
php将 xm
l解析成array - 实现excel数据的导入导出 thinkphp
- php判断pc端和手机端
- php上传文件
- php订单管理系统,附赠设计报告
- 微直播(php直播源码)
- ThinkPHP 课程管理系统源码含sql文件
- php 企业建站系统源码zzzcms
- php评测系统源码含数据库脚本
- 公司网站,企业网站 CodeIgniter,PHP
- 百度贴吧签到助手PHP源码
- 生成验证码
- php实现进度条
- php cms系统(dede-cms)
- thinkphp教学
- thinkPHP微信基础类
- 然之协同管理系统ranzhi.4.0
评论
共有 条评论