php Webコンテンツの取得


キャプチャされた内容は
正規表現をフィルタリングすると、欲しい内容が得られます.
正規表現フィルタリングは、ここでは紹介しませんが、興味があるのは、phpでWebページの内容をキャプチャする方法です.
1.file_get_contents PHPコード
 
  
<?php
$url = "http://www.jb51.net";
$contents = file_get_contents($url);
//
//$getcontent = iconv("gb2312", "utf-8",$contents);
echo $contents;
?>

2.curl
PHPコード
 
  
<?php
$url = "http://www.jb51.net";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//
//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.jb51.net", "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,allow_を設定するurl_fopen = On,allow_url_fopenが閉じるとfopenとfile_get_contentsはリモートファイルを開くことができません.
2.curlを使用するには、curlを開くスペースが必要です.方法:windowsの下でphpを修正します.ini,extension=php_curl.dllの前のセミコロンは削除され、ssleay 32をコピーする必要がある.dllとlibeay 32.dllからC:WINDOWSsystem 32下;Linuxの下にcurl拡張子をインストールします.