This is a translated page. The original can be found here: http://iwebdevel.com/2009/10/03/php-how-to-download-a-webpage-aka-web-scrapping-with-php-fsockopen-file_get_contents-curl-function-download-web-page/
UPDATES VIA RSS | Email UPDATES VIA RSS | E-pasts Get updates via feedburner Get updates via twitter
Home / Coding / PHP / PHP: How to download a webpa… Home / Coding / PHP / PHP: Kā lejupielādēt webpa ...

PHP: How to download a webpage (aka web scrapping) with PHP PHP: Kā lejupielādēt tīmekļa vietnē (pazīstams arī kā web metāllūžņos) ar PHP

Posted on 03. Posted on 03. Oct, 2009 by Dragos in Coding , PHP Okt, 2009 by Dragos in kodēšanas, PHP

There are many ways of downloading web pages, or web content. Ir daudz veidu, kā lejupielādēt Web lapas, vai tīmekļa saturu. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Personīgi es vēlētos izmantot čokurošanās manu web metāllūžņos vajadzībām, bet reizēm man arī izmantot fsockopen un file_get_contents.

Here are 3 different functions that will allow you to download web content. Šeit ir 3 dažādas funkcijas, kas ļaus jums ielādēt interneta saturu.

cURL : čokurošanās:

 function getData($url) { funkcija getData ($ 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) Gecko/2008070208 Firefox/3.0.3");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,3); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 3);
        $result['data']=curl_exec($ch); $ result [ 'dati'] = 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'] = 'kļūdīties';
} ) 

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; } } funkcija getData ($ url) ($ arr = parse_url ($ url); $ fp = fsockopen ($ arr [ 'uzņēmēja'], 80, $ errno, $ errstr, 30); if (! $ fp) (return false; ) else (/ / sūtīt headers $ out = "GET". fsockopen ($ arr [ 'uzņēmēja'], 80, $ errno, $ errstr, 30). "HTTP/1.1 \ r \ n"; $ out .= " Host: ". str_replace ( 'http://'. $ arr [" uzņēmēja'],'',$ URL). "\ r \ n"; $ out .= "User-Agent: FSOCKOPEN \ r \ n" $ out .= "Connection: Close \ r \ n \ r \ n"; fwrite ($ fp, $ out); while (! feof ($ fp)) ($ saturs .= fgets ($ fp, 4096); ); fclose ($ fp); return $ saturs;)) 

file_get_contents file_get_contents

 function getData($url) { funkcija getData ($ url) (
return file_get_contents($url); atpakaļ 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. Kā redzat, vieglākais veids, kā lejupielādēt interneta saturu, izmantojot file_get_contents funkciju, bet, ja jums nepieciešama papildu iespējas, īpaši, ja jūs strādājat ar galvenēm, tad čokurošanās ir labākais veids, kā iet jums.

Translate this post Tulkot šo ziņu


Related posts: Related posts:

  1. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Sūtīt funkcija kā parametru citai funkcijai (callbacks)

  • saya kemarin juga ngerjain web yang grabb data dari website lain... saya kemarin juga ngerjain web yang grabb datu Dari mājas lain ...

    sayang ga sempat baca artikel ini, jadi nya pake file_get_contents().. sayang ga sempat lasīt artikel ini, jadi nya pake file_get_contents () .. di potong2 pake preg_replace(), buat ngambil data yang di butuh kan... di potong2 pake preg_replace (), buat ngambil datu yang di butuh kan ...


    btw salam kenal... btw Salam kenal ...
blog comments powered by Disqus blog komentāriem powered by Disqus