資源簡介
<?php
function curl_get_file_contents($url,$referer='') {
set_time_limit(0);
static $curl_loops = 0;//避免死了循環(huán)必備
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); //不驗(yàn)證證書
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不驗(yàn)證證書
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> 區(qū)段
// <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 = "無法解析數(shù)據(jù)中的 <head> 區(qū)段";
}
// 取得 <head> 中 meta 設(shè)置的編碼格式<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> 的內(nèi)容";
exit;
}
// 將<title> 的文字編碼格式轉(zhuǎn)成 UTF-8
if($charset == "None"){
$title=$htmlTitles[1];
}else{
$title=iconv($charset, "UTF-8", $htmlTitles[1]);
}
}
return html_entity_decode($title);
}
/*網(wǎng)址寫入*/
$urla = array('這里填入數(shù)組網(wǎng)址'
);
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;//避免死了循環(huán)必備
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); //不驗(yàn)證證書
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不驗(yàn)證證書
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> 區(qū)段
// <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 = "無法解析數(shù)據(jù)中的 <head> 區(qū)段";
}
// 取得 <head> 中 meta 設(shè)置的編碼格式<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> 的內(nèi)容";
exit;
}
// 將<title> 的文字編碼格式轉(zhuǎn)成 UTF-8
if($charset == "None"){
$title=$htmlTitles[1];
}else{
$title=iconv($charset, "UTF-8", $htmlTitles[1]);
}
}
return html_entity_decode($title);
}
/*網(wǎng)址寫入*/
$urla = array('這里填入數(shù)組網(wǎng)址'
);
foreach ($urla as $key => $url) {
$html = curl_get_file_contents($url);
$title = get_title_contents($html);
echo $title;
echo '</br>';
}
代碼片段和文件信息
- 上一篇:upload-lab靶場PHP源碼
- 下一篇:php獲取企業(yè)微信用戶信息
評論
共有 條評論
相關(guān)資源
- php獲取企業(yè)微信用戶信息
- upload-lab靶場PHP源碼
- ucms_1.4.8 php源碼
- 微信小程序支付付款Deom(php|小程序代
- PHP圖片驗(yàn)證碼案列
- 學(xué)生信息登記表(php代碼)
- php上傳、編輯用戶頭像
- PHP許愿墻(源碼+數(shù)據(jù)庫)
- php查詢天氣預(yù)報
- 公司核名系統(tǒng)(php源碼)
-
php excel 操作源碼 xm
l - php 日歷控件(DATE PICKER - JQUERY PLUGIN)
- php 日歷(bootstrap-datetimepicker)
-
php將 xm
l解析成array - 實(shí)現(xiàn)excel數(shù)據(jù)的導(dǎo)入導(dǎo)出 thinkphp
- php判斷pc端和手機(jī)端
- php上傳文件
- php訂單管理系統(tǒng),附贈設(shè)計報告
- 微直播(php直播源碼)
- ThinkPHP 課程管理系統(tǒng)源碼含sql文件
- php 企業(yè)建站系統(tǒng)源碼zzzcms
- php評測系統(tǒng)源碼含數(shù)據(jù)庫腳本
- 公司網(wǎng)站,企業(yè)網(wǎng)站 CodeIgniter,PHP
- 百度貼吧簽到助手PHP源碼
- 生成驗(yàn)證碼
- php實(shí)現(xiàn)進(jìn)度條
- php cms系統(tǒng)(dede-cms)
- thinkphp教學(xué)
- thinkPHP微信基礎(chǔ)類
- 然之協(xié)同管理系統(tǒng)ranzhi.4.0