PHP獲取網(wǎng)頁所有連接的方法(附demo源碼下載)
本文實例講述了PHP獲取網(wǎng)頁所有連接的方法。分享給大家供大家參考,具體如下:
function getHtml($url, $charset='utf-8')
{
$curl = curl_init();
//curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:192.168.168.1', 'CLIENT-IP:192.168.168.1'));//IP
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_REFERER, ""); //來路
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
// 只需返回HTTP header
// curl_setopt($curl, CURLOPT_HEADER, 1);
// 頁面內(nèi)容我們并不需要
// curl_setopt($curl, CURLOPT_NOBODY, 1);
// 返回結(jié)果,而不是輸出它
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($curl);
//$info = curl_getinfo($curl);
//echo var_dump($info);
if ($html === false) {
//echo "cURL Error: " . curl_error($ch);
return '';
}
curl_close($curl);
if ($charset != 'utf-8')
{
$html = iconv($charset, "UTF-8", $html);
}
return $html;
}
header("Content-type: text/html; charset=utf-8");
include('simple_html_dom.php');
// 要打開 extension=php_mbstring.dll
//$url = 'http://www.baidu.com/s?wd=kaka';
$url = 'http://www.163.com/';
$str_html = getHtml($url, 'gbk');
$html = str_get_html($str_html);
$links = $html->find('a');
foreach($links as $link)
{
$txt = trim($link->plaintext);
echo $link->href . '[' . $txt . ']<br>';
}
$html = null;
完整實例代碼點擊此處本站下載。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡編程技巧總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關(guān)文章
php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法
這篇文章主要介紹了php版微信支付api.mch.weixin.qq.com域名解析慢原因與解決方法,詳細分析了微信支付api.mch.weixin.qq.com域名解析慢原因與使用curl_easy_setopt指定ipv4解決ipv6解析問題的相關(guān)技巧,需要的朋友可以參考下2016-10-10
PHP多維數(shù)組轉(zhuǎn)一維數(shù)組的簡單實現(xiàn)方法
這篇文章主要介紹了PHP多維數(shù)組轉(zhuǎn)一維數(shù)組的簡單實現(xiàn)方法,涉及PHP遞歸操作技巧,簡單實用,需要的朋友可以參考下2015-12-12
PHP實現(xiàn)的redis主從數(shù)據(jù)庫狀態(tài)檢測功能示例
這篇文章主要介紹了PHP實現(xiàn)的redis主從數(shù)據(jù)庫狀態(tài)檢測功能,涉及php針對多個redis主從數(shù)據(jù)庫的連接、檢測、錯誤信息輸出及郵件發(fā)送相關(guān)操作技巧,需要的朋友可以參考下2017-07-07

