PHP: How to download a webpage (aka web scrapping) with PHP PHPの:どのようにウェブページをPHPで(別名 : ウェブの廃止)をダウンロードする
Posted on 03. 03に掲載した。 Oct, 2009 by Dragos in Coding , PHP 10月、2009 のコーディング 、PHPのでドラゴシュで
There are many ways of downloading web pages, or web content.ようこそ、Webページ、またはWebコンテンツをダウンロードする多くの方法があります。 Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents .個人的に私のcURLウェブスクラップニーズに合わせて使用しているような時々私はまた、fsockopenおよび file_get_contentsを使用します。
Here are 3 different functions that will allow you to download web content.ここで、あなたをダウンロードするWebコンテンツを許可する3つの異なる関数があります。
cURL : のcURL:
function getData($url) {関数getDataから($のURL)( if($url!='localhost' && $url!='http://localhost') { ($場合はURLを!='&&$ローカルホストのURL!='http://localhostを')( $ch=curl_init(); $チャンネル= curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($チャンネル、CURLOPT_URLが、$のURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($チャンネル、CURLOPT_RETURNTRANSFERが、がTRUE); curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.3"); curl_setopt($チャンネル、CURLOPT_USERAGENT、"Mozilla/6.0(Windowsの;ロ;は、Windows NT 5.1;アン米;右心室:1.9.0.1)Gecko/2008070208 Firefox/3.0.3"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,3); curl_setopt($チャンネル、CURLOPT_FOLLOWLOCATIONと、3); $result['data']=curl_exec($ch); $結果['データ'] = curl_exec($チャンネル); $result['error']=curl_error($ch); $結果['エラー'] = curl_error($チャンネル); curl_close($ch);しますcurl_close($チャンネル); return $result; $ resultを返す; } ) else return $result['error']='err';他の$ resultを返す['エラー'] ='ERRを'; } )
fsockopen fsockopen
function getData($url) { $arr=parse_url($url); $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); if(!$fp) { return false; }else { // send headers $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $out .= "Host: ".str_replace('http://'.$arr['host'],'',$url)."\r\n"; $out .= "User-Agent: FSOCKOPEN\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while(!feof($fp)) { $contents .= fgets($fp, 4096); }; fclose($fp); return $contents; } }関数getDataから($のURL)($ arr = parse_url($ URL)を$ fPは= fsockopen($ arr ['ホスト']、80、$ errnoは、$ errstr、30);場合($ FP)を(falseを返す;アウト="のGET)他(/ /"のヘッダ$を送信します。fsockopen($ arr ['ホスト']、80、$ errnoは、$ errstr、30)を"HTTP/1.1 \ Rは\ Nで"; $ .=アウト"ホスト:"。str_replace('http://'を、$ arr ['ホスト'],'',$のurl)。"\ Rは\ Nで"; $ .=アウト"のUser - Agent:FSOCKOPEN \ Rは\ Nで" $ .=アウト"接続:閉じる\ Rは\ Nの\ Rは\ Nで";($ fPは、$を)しながら(!feof($ FP)を)($内容.= fgets($ fPは、4096)fwrite; ); FP)を($ fclose $内容;を返す)) file_get_contents file_get_contents
function getData($url) {関数getDataから($のURL)( return file_get_contents($url);戻りfile_get_contents($のURL); } )
As you see the easiest way of downloading web content is by using the file_get_contents function, but if you need more options, especially if you are working with the headers, then cURL is the best way to go for you.し、cURLのようなWebコンテンツをダウンロードする最も簡単な方法を参照してfile_get_contents関数を使用してですが、場合は、特に場合は、ヘッダーで作業して詳細オプションが必要な場合のために行くための最良の方法です。
Related posts:関連記事:
- ferry ardhana フェリーardhana












































