PHP: How to download a webpage (aka web scrapping) with PHP PHP: Ako sťahovať webové stránky (aj web zošrotovanie) s PHP
Posted on 03. Publikované dňa 03. Oct, 2009 by Dragos in Coding , PHP Októbra 2009 od Dragos v Kódovanie, PHP
There are many ways of downloading web pages, or web content. Existuje mnoho spôsobov, ako sťahovať z webových stránok či obsahu webových stránok. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Osobne rád používam pre moje potreby cURL web zošrotovania, ale niekedy sa tiež použiť fsockopen a file_get_contents.
Here are 3 different functions that will allow you to download web content. Tu sú 3 rôzne funkcie, ktoré vám umožní sťahovať obsah webových stránok.
cURL : cURL:
function getData($url) { if($url!='localhost' && $url!='http://localhost') { $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, 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($ch, CURLOPT_FOLLOWLOCATION,3); $result['data']=curl_exec($ch); $result['error']=curl_error($ch); curl_close($ch); return $result; } else return $result['error']='err'; } Funkcia getdata ($ url) (if ($ url! = 'Localhost' & & $ url! = 'Http://localhost') ($ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ( $ ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/6.0 (compatible; Windows NT 5.1; en-US; rv: 1.9.0.1) Gecko/2008070208 Firefox/3.0.3"); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 3); $ result [ 'data'] = curl_exec ($ ch); $ result [ 'error'] = curl_error ($ ch); curl_close ($ ch); return $ result;) else return $ result [ 'error'] = 'err';) fsockopen fsockopen
function getData($url) { Funkcia getdata ($ url) ( $arr=parse_url($url); $ Arr = parse_url ($ url); $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); $ Fp = fsockopen ($ arr [ 'host'], 80, $ errno, $ errstr, 30); if(!$fp) { if ($ fp) ( return false; return false; }else { else () // send headers / / Odoslanie hlaviček $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $ 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 .= "Host:". Str_replace ( 'http://'. $ Arr [ 'host'],'',$ URL). "\ R \ n"; $out .= "User-Agent: FSOCKOPEN\r\n"; $ Out .= "User-Agent: fsockopen \ r \ n"; $out .= "Connection: Close\r\n\r\n"; $ Out .= "Connection: Close \ r \ n \ r \ n"; fwrite($fp, $out); fwrite ($ fp, $ out); while(!feof($fp)) { while (! feof ($ fp)) ( $contents .= fgets($fp, 4096); $ Obsah .= fgets ($ fp, 4096); }; ); fclose($fp); fclose ($ fp); return $contents; return $ obsah; } ) } )
file_get_contents file_get_contents
function getData($url) { Funkcia getdata ($ url) ( return file_get_contents($url); návrat 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. Ako vidíte, je najjednoduchšie sťahovanie obsahu webu je pomocou funkcie file_get_contents, ale ak potrebujete viac možností, najmä ak pracujete s hlavičky, potom cURL je najlepšia cesta pre vás.
Related posts: Súvisiace príspevky:
- ferry ardhana trajekt ardhana












































