PHP: How to download a webpage (aka web scrapping) with PHP PHP: Com descarregar una pàgina web (també conegut com el desballestament de la web) amb PHP
Posted on 03. Publicat a 03. Oct, 2009 by Dragos in Coding , PHP Octubre de 2009, per Dragos a la codificació, PHP
There are many ways of downloading web pages, or web content. Hi ha moltes maneres de descarregar pàgines web o el contingut web. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Personalment m'agrada utilitzar CURL per a les meves necessitats de desguàs web, però de vegades també utilitzen fsockopen i file_get_contents.
Here are 3 different functions that will allow you to download web content. Aquí hi ha 3 diferents funcions que li permetrà descarregar contingut web.
cURL : cURL:
function getData($url) { GetDate funció ($ url) ( if($url!='localhost' && $url!='http://localhost') { if ($ url! = 'localhost' & & $ url! = 'http://localhost') ( $ch=curl_init(); $ Ch = curl_init (); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 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_USERAGENT, "Mozilla/6.0 (compatible; Windows NT 5.1; en-US; rv: 1.9.0.1) Firefox/3.0.3 Gecko/2008070208"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,3); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 3); $result['data']=curl_exec($ch); $ Result [ 'data'] = curl_exec ($ ch); $result['error']=curl_error($ch); $ Result [ 'error'] = curl_error ($ ch); curl_close($ch); curl_close ($ ch); return $result; return $ result; } ) else return $result['error']='err'; else return $ result [ 'error'] = 'errar'; } )
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; } } GetDate funció ($ url) ($ matriu = parse_url ($ url); $ fp = fsockopen ($ matriu [ 'host'], 80, FP $ errno, $ errstr, 30); if ($) (return false; ) else (/ / enviar les capçaleres de $ out = "GET". fsockopen ($ matriu [ 'host'], 80, $ errno, $ errstr, 30). "HTTP/1.1 \ r \ n"; $ sortida .= " host: ". str_replace ( 'http://'. $ matriu [ 'url'],'',$ d'acollida)." \ r \ n "; $ sortida .=" User-Agent: fsockopen \ r \ n " ; $ sortida .= "Connection: close \ r \ n \ r \ n"; fwrite ($ fp, $ out), mentre que (! feof ($ fp)) ($ contingut .= fgets ($ fp, 4096); ); fclose ($ fp); return $ contingut;)) file_get_contents file_get_contents
function getData($url) { GetDate funció ($ url) ( return file_get_contents($url); file_get_contents return ($ 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. Com es pot veure la forma més fàcil de descarregar contingut web és mitjançant la funció file_get_contents, però si necessita més opcions, sobretot si es treballa amb les capçaleres, a continuació, cURL és la millor manera d'anar per a tu.
Related posts: Llocs relacionats amb:
- ferry ardhana ardhana ferri












































