php獲取網(wǎng)頁內(nèi)容方法總結(jié)
更新時間:2008年12月04日 13:49:51 作者:
用php抓取頁面的內(nèi)容在實際的開發(fā)當(dāng)中是非常有用的,如作一個簡單的內(nèi)容采集器,提取網(wǎng)頁中的部分內(nèi)容等等
抓取到的內(nèi)容在通過正則表達式做一下過濾就得到了你想要的內(nèi)容,至于如何用正則表達式過濾,在這里就不做介紹了,有興趣的,以下就是幾種常用的用php抓取網(wǎng)頁中的內(nèi)容的方法。
1.file_get_contents
PHP代碼
<?php
$url = "http://www.dhdzp.com";
$contents = file_get_contents($url);
//如果出現(xiàn)中文亂碼使用下面代碼
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>
2.curl
PHP代碼
<?php
$url = "http://www.dhdzp.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用戶檢測的網(wǎng)頁里需要增加下面兩行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
3.fopen->fread->fclose
PHP代碼
<?php
$handle = fopen ("http://www.dhdzp.com", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>
注:
1.使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設(shè)置allow_url_fopen = On,allow_url_fopen關(guān)閉時fopen和file_get_contents都不能打開遠程文件。
2.使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號去掉,而且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴展。
1.file_get_contents
PHP代碼
復(fù)制代碼 代碼如下:
<?php
$url = "http://www.dhdzp.com";
$contents = file_get_contents($url);
//如果出現(xiàn)中文亂碼使用下面代碼
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>
2.curl
PHP代碼
復(fù)制代碼 代碼如下:
<?php
$url = "http://www.dhdzp.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用戶檢測的網(wǎng)頁里需要增加下面兩行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
?>
3.fopen->fread->fclose
PHP代碼
復(fù)制代碼 代碼如下:
<?php
$handle = fopen ("http://www.dhdzp.com", "rb");
$contents = "";
do {
$data = fread($handle, 1024);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($handle);
echo $contents;
?>
注:
1.使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設(shè)置allow_url_fopen = On,allow_url_fopen關(guān)閉時fopen和file_get_contents都不能打開遠程文件。
2.使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號去掉,而且需要拷貝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安裝curl擴展。
您可能感興趣的文章:
- thinkphp3.x中變量的獲取和過濾方法詳解
- php完全過濾HTML,JS,CSS等標(biāo)簽
- php過濾HTML標(biāo)簽、屬性等正則表達式匯總
- php 安全過濾函數(shù)代碼
- PHP對表單提交特殊字符的過濾和處理方法匯總
- 過濾掉PHP數(shù)組中的重復(fù)值的實現(xiàn)代碼
- PHP屏蔽過濾指定關(guān)鍵字的方法
- php獲取數(shù)組長度的方法(有實例)
- php自動獲取字符串編碼函數(shù)mb_detect_encoding
- php獲取post中的json數(shù)據(jù)的實現(xiàn)方法
- php 如何獲取數(shù)組第一個值
- php通過兩層過濾獲取留言內(nèi)容的方法
相關(guān)文章
php模擬socket一次連接,多次發(fā)送數(shù)據(jù)的實現(xiàn)代碼
php模擬socket一次連接,多次發(fā)送數(shù)據(jù)的實現(xiàn)代碼,需要的朋友可以參考下。2011-07-07
php使用function_exists判斷函數(shù)可用的方法
這篇文章主要介紹了php使用function_exists判斷函數(shù)可用的方法,通過一個圖像處理函數(shù)中使用function_exists函數(shù)判斷并輸出來實現(xiàn)函數(shù)存在判斷與流程靈活控制的功能,具有很好的借鑒價值,需要的朋友可以參考下2014-11-11
Win2003下IIS+PHP+MySQL+Zend配置步驟詳解
Win2003下IIS+PHP+MySQL+Zend配置步驟詳解...2007-05-05
PHP中l(wèi)trim與rtrim去除左右空格及特殊字符實例
這篇文章主要介紹了PHP中l(wèi)trim與rtrim去除左右空格及特殊字符的用法,結(jié)合實例形式較為詳細的說明了ltrim與rtrim的定義,參數(shù)作用及去除左右空格與特殊字符的使用技巧,需要的朋友可以參考下2016-01-01

