php中採集抓取頁面函數詳解_PHP教程

來源:互聯網
上載者:User
在php中提供了大量的擷取遠程伺服器檔案的函數,包括有:file()函數、file_get_contents()函數、fopen()->fread()->fclose()模式、curl方式、fsockopen()函數、socket模式等等,下面我來分別來介紹介紹。

1. file()函數

file() 函數把整個檔案讀入一個數組中。

與 file_get_contents() 類似,不同的是 file() 將檔案作為一個數組返回。數組中的每個單元都是檔案中相應的一行,包括分行符號在內。

如果失敗,則返回 false。

代碼如下 複製代碼


$url='http://www.bKjia.c0m';
$lines_array=file($url);
$lines_string=implode('',$lines_array);
echo htmlspecialchars($lines_string);

?>

2. file_get_contents()函數


file_get_contents() 函數把整個檔案讀入一個字串中。

和 file() 一樣,不同的是 file_get_contents() 把檔案讀入一個字串。

file_get_contents() 函數是用於將檔案的內容讀入到一個字串中的首選方法。如果作業系統支援,還會使用記憶體映射技術來增強效能。

代碼如下 複製代碼

$url='http://www.bKjia.c0m';
$lines_string=file_get_contents($url);
echo htmlspecialchars($lines_string);

?>

使用file_get_contents和fopen必須空間開啟allow_url_fopen。方法:編輯php.ini,設定 allow_url_fopen = On,allow_url_fopen關閉時fopen和file_get_contents都不能開啟遠程檔案。

3. fopen()->fread()->fclose()模式

代碼如下 複製代碼

$url='http://www.bKjia.c0m';
$handle=fopen($url,"rb");
$lines_string="";
do{
$data=fread($handle,1024);
if(strlen($data)==0) {
break;
}
$lines_string.=$data;
}while(true);
fclose($handle);
echo htmlspecialchars($lines_string);

4. curl方式

使用curl必須空間開啟curl。方法:windows下修改php.ini,將extension=php_curl.dll前面的分號去掉,而且需 要拷貝ssleay32.dll和libeay32.dll到C:WINDOWSsystem32下;Linux下要安裝curl擴充。

代碼如下 複製代碼

$url='http://www.bKjia.c0m';
$ch=curl_init();
$timeout=5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$lines_string=curl_exec($ch);
curl_close($ch);
echo htmlspecialchars($lines_string);


5. fsockopen()函數 socket模式
socket模式能否正確執行,也跟伺服器的設定有關係,具體可以通過phpinfo查看伺服器開啟了哪些通訊協定,比如我的本地php socket沒開啟http,只能使用udp測試一下了。

還有一個以curl_開頭的函數,可以實現很多功能。有時間要好好研究!下面是關於fscokopen的介紹


1.PHP fsockopen函數說明:

Open Internet or Unix domain socket connection(開啟通訊端連結)

Initiates a socket connection to the resource specified by target .

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一個檔案控制代碼

開啟PHP fsockopen這個函數

PHP fsockopen需要 PHP.ini 中 allow_url_fopen 選項開啟。

代碼如下 複製代碼

set_time_limit(0);
$fp = fsockopen("www.hzhuti.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
n";
} else {
$out = "POST / HTTP/1.1rn";
$out .= "Host: www.bKjia.c0mrn";
$out .= "Connection: Closernrn";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

http://www.bkjia.com/PHPjc/630706.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630706.htmlTechArticle在php中提供了大量的擷取遠程伺服器檔案的函數,包括有:file()函數、file_get_contents()函數、fopen()->fread()->fclose()模式、curl方式、fsockopen()函數...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.