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-mail Get updates via feedburner Get updates via twitter
Home / Coding / PHP / PHP: How to download a webpa… Home / Coding / PHP / PHP: Hoe te downloaden van een webpa ...

PHP: How to download a webpage (aka web scrapping) with PHP PHP: Hoe te downloaden van een webpagina (aka web sloop) met PHP

Posted on 03. Geplaatst op 03. Oct, 2009 by Dragos in Coding , PHP Oktober, 2009 door Dragos in Coding, PHP

There are many ways of downloading web pages, or web content. Er zijn vele manieren van het downloaden van webpagina's, of web content. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Persoonlijk ben ik graag cURL gebruiken voor mijn behoeften web sloop, maar soms heb ik ook fsockopen en file_get_contents.

Here are 3 different functions that will allow you to download web content. Hier zijn 3 verschillende functies waarmee u het downloaden van webinhoud.

cURL : cURL:

 function getData($url) { functie 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 (compatible; 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 [ 'data'] = curl_exec ($ ch);
        $result['error']=curl_error($ch); $ resultaat [ 'error'] = curl_error ($ ch);
        curl_close($ch); curl_close ($ ch);
        return $result; return $ resultaat;
    } )
    else return $result['error']='err'; else return $ result [ 'error'] = 'err';
} ) 

fsockopen fsockopen

 function getData($url) { functie 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 / / Headers sturen
        $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); $ contents .= fgets ($ fp, 4096);
        }; );
        fclose($fp); fclose ($ fp);
        return $contents; return $ inhoud;
    } )
} ) 

file_get_contents file_get_contents

 function getData($url) { functie GetData ($ 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. Als je de eenvoudigste manier van downloaden van web content te zien is via de file_get_contents functie, maar als u meer mogelijkheden, vooral als u werkt met de kop, dan cURL is de beste manier om te gaan voor je.

Translate this post Translate this post


Related posts: Gerelateerde berichten:

  1. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Stuur functie als parameter aan een andere functie (callbacks)

  • saya kemarin juga ngerjain web yang grabb data dari website lain... kemarin saya juga ngerjain web yang Grabb gegevens lain dari website ...

    sayang ga sempat baca artikel ini, jadi nya pake file_get_contents().. Sayang ga sempat Baca 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 gegevens yang di butuh kan ...


    btw salam kenal... btw salam kenal ...
blog comments powered by Disqus blog comments powered by Disqus