PHP: How to download a webpage (aka web scrapping) with PHP PHP: Si te shkarkoj nje faqe (aka hequr web) me PHP
Posted on 03. Dërguar të 03. Oct, 2009 by Dragos in Coding , PHP Tetor, 2009 nga Dragos në kodim, PHP
There are many ways of downloading web pages, or web content. Ka shumë mënyra e shkarkimit web pages, apo përmbajtje web. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Personalisht unë si të përdorin për të hequr curl nevojat e mia web, por nganjëherë edhe unë përdorni fsockopen dhe file_get_contents.
Here are 3 different functions that will allow you to download web content. Këtu janë 3 funksione të ndryshme që do t'ju lejojë të përmbajtjes download 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 [ 'dhënave'] = curl_exec ($ ch); $result['error']=curl_error($ch); $ result [ 'gabim'] = curl_error ($ ch); curl_close($ch); curl_close ($ ch); return $result; return $ rezultat; } ) else return $result['error']='err'; else return $ rezultat [ 'gabim'] = 'bëj gabim'; } )
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 / Dërgoni 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"; $ nga .= "Host:". str_replace ( 'http://'. $ arr [ 'host'],'',$ url). "\ r \ n"; $out .= "User-Agent: FSOCKOPEN\r\n"; $ nga .= "User-Agent: FSOCKOPEN \ r \ n"; $out .= "Connection: Close\r\n\r\n"; $ nga .= "Connection: Close \ r \ n \ r \ n"; fwrite($fp, $out); fwrite ($ FP, $ jashtë); while(!feof($fp)) { while (! feof ($ FP)) ( $contents .= fgets($fp, 4096); Përmbajtja $ .= fgets ($ FP, 4096); }; ); fclose($fp); fclose ($ FP); return $contents; return $ përmbajtja; } ) } )
file_get_contents file_get_contents
function getData($url) { getData function ($ 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. Si e shihni mënyra më e lehtë të përmbajtjes së shkarkimit web duke përdorur funksionin file_get_contents, por në qoftë se keni nevojë për opsione më shumë, sidomos nëse jeni duke punuar me headers, atëherë curl është mënyra më e mirë për të shkuar për ju.
No related posts. Asnjë mesazh i lidhur.
- ferry ardhana ardhana traget












































