PHP: How to download a webpage (aka web scrapping) with PHP PHP: Comment faire pour télécharger une page Web (alias démolition web) avec PHP
Posted on 03. Posté le 03. Oct, 2009 by Dragos in Coding , PHP Oct, 2009 par Dragos en codage, PHP
There are many ways of downloading web pages, or web content. Il ya plusieurs façons de télécharger des pages Web ou du contenu web. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Personnellement, j'aime utiliser cURL pour mes besoins web de déchirage, mais parfois je aussi utiliser fsockopen et file_get_contents.
Here are 3 different functions that will allow you to download web content. Voici 3 fonctions différentes qui vous permettra de télécharger du contenu web.
cURL : cURL:
function getData($url) { getData function ($ 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 (Windows; U; 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'] = 'erreur'; } )
fsockopen fsockopen
function getData($url) { getData function ($ 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 / / Send headers $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 [ 'url'],'',$ hôte). "\ 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); $ contents .= fgets ($ fp, 4096); }; ); fclose($fp); fclose ($ fp); return $contents; return $ contents; } ) } )
file_get_contents file_get_contents
function getData($url) { getData function ($ url) ( return file_get_contents($url); return 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. Comme vous le voyez la façon la plus simple de télécharger du contenu Web est d'utiliser la fonction file_get_contents, mais si vous avez besoin de plus d'options, surtout si vous travaillez avec les en-têtes, puis cURL est la meilleure voie à suivre pour vous.
No related posts. Pas de postes.
- ferry ardhana ferry ardhana












































