PHP讀取網(wǎng)頁文件內(nèi)容的實現(xiàn)代碼(fopen,curl等)
更新時間:2011年06月23日 13:22:12 作者:
php小偷程序中經(jīng)常需要獲取遠程網(wǎng)頁的內(nèi)容,下面是一些實現(xiàn)代碼,需要的朋友可以慘況下。
1.fopen實現(xiàn)代碼:
<?php
$handle = fopen ("http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
<?php
// 對 PHP 5 及更高版本
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
2.curl實現(xiàn)代碼:
<?php
function _url($Date){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$pageURL="http://www.baidu.com";
$contents=_url($pageURL);
?>
編碼轉(zhuǎn)換函數(shù)
$html = file_get_contents("http://s.jb51.net");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //轉(zhuǎn)化編碼方式為UTF8
print $html;
$htm = file("http://s.jb51.net");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312", "utf-8//IGNORE" , $value);
}
print_r($h);
另一種打開網(wǎng)頁的方法
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen('http://www.baidu.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>
復(fù)制代碼 代碼如下:
<?php
$handle = fopen ("http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
復(fù)制代碼 代碼如下:
<?php
// 對 PHP 5 及更高版本
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
2.curl實現(xiàn)代碼:
復(fù)制代碼 代碼如下:
<?php
function _url($Date){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}
$pageURL="http://www.baidu.com";
$contents=_url($pageURL);
?>
編碼轉(zhuǎn)換函數(shù)
復(fù)制代碼 代碼如下:
$html = file_get_contents("http://s.jb51.net");
$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //轉(zhuǎn)化編碼方式為UTF8
print $html;
$htm = file("http://s.jb51.net");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312", "utf-8//IGNORE" , $value);
}
print_r($h);
另一種打開網(wǎng)頁的方法
復(fù)制代碼 代碼如下:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp = fopen('http://www.baidu.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>
相關(guān)文章
關(guān)于URL最大長度限制的相關(guān)資料查證
這篇文章主要介紹了關(guān)于URL最大長度限制的相關(guān)資料查證,這里記錄一下,方便以后使用。2014-12-12
關(guān)于mysql字符集設(shè)置了character_set_client=binary 在gbk情況下會出現(xiàn)表描述是亂碼的情況
今天在處理bug的時候,又發(fā)現(xiàn)一個奇怪的問題,在設(shè)置mysql的編碼為gbk的情況下,發(fā)現(xiàn)建立出來的表,凡是表結(jié)構(gòu)無論是字段還是表結(jié)構(gòu)描述是中文的時候,建立出來的表結(jié)構(gòu)描述是亂碼,經(jīng)過排查原來是character_set_client=binary惹的禍2013-01-01
php自定義函數(shù)br2nl實現(xiàn)將html中br換行符轉(zhuǎn)換為文本輸入中換行符的方法【與函數(shù)nl2br功能相反】
這篇文章主要介紹了php自定義函數(shù)br2nl實現(xiàn)將html中br換行符轉(zhuǎn)換為文本輸入中換行符的方法,具有與函數(shù)nl2br相反的功能,并附帶了相應(yīng)的JS實現(xiàn)方法,需要的朋友可以參考下2017-02-02
PHP 使用Echarts生成數(shù)據(jù)統(tǒng)計報表的實現(xiàn)代碼
這篇文章主要介紹了PHP 使用Echarts生成數(shù)據(jù)統(tǒng)計報表的實現(xiàn)代碼,需要的朋友可以參考下2018-01-01

