1.fopen實作碼:
複製程式碼 程式碼如下:
$handle = fopen (" http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle , 8192);
}
fclose($handle);
?>
複製程式碼
複製程式碼
碼:
// 對PHP 5 及更高版本
$handle = fopen("http://www.example.com/", "rb") ;
2.curl實現代碼:
複製程式碼
程式碼如下:
function _url($Date){
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, "$Date");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($4, CUR37, 407_PTLilla:407_/4:407_48F/48F/48F/48_F/Fle) (M7_F; ; 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";
編碼轉換函數
複製程式碼
程式碼如下:
$html = file_get_contents("http://s.jb51.net"); 🎜>$html = iconv( "Big5", "UTF-8//IGNORE" , $html); //轉換編碼方式為UTF8
print $html;
$htm = file("http:/ /s.jb51.net");
$h = "";
foreach($htm as $value)
{
$h.= iconv( "GB2312", "utf-8 //IGNORE" , $value);
}
另一種打開網頁的方法複製程式碼
程式碼如下:
$opts = array(
'http'=>array(
'method'=>" GET",
'header'=>"Accept-language: enrn" .
"Cookie: foo=barrn"
)
);
$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);
以上就介紹了fop PHP讀取網頁檔案內容的實作程式碼fopen,curl等,包含了fop方面的內容,希望對PHP教學有興趣的朋友有幫助。