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 Actualizacións Via RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / PHP / PHP: How to download a webpa… Home / Codificación / PHP / PHP: Como baixar un webpa ...

PHP: How to download a webpage (aka web scrapping) with PHP PHP: Como baixar unha páxina web (web demolición aka) con PHP

Posted on 03. Posta en 03. Oct, 2009 by Dragos in Coding , PHP Outubro de 2009 por Dragos na Codificación, PHP

There are many ways of downloading web pages, or web content. Hai moitas formas de baixar páxinas web ou contidos da web. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Persoalmente eu gusto de usar CURL a miñas necesidades demolición da web, pero ás veces eu tamén uso fsockopen e file_get_contents.

Here are 3 different functions that will allow you to download web content. Aquí son 3 diferentes funcións que permitirá que baixar o contido da web.

cURL : CURL:

 function getData($url) { function getData ($ url) (
    if($url!='localhost' && $url!='http://localhost') { if ($ url! = 'localhost' & & $ url! = 'http://localhost') (
        $ch=curl_init(); $ X = 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); $ Resultado [ 'data'] = curl_exec ($ ch);
        $result['error']=curl_error($ch); $ Result [ 'error'] = curl_error ($ ch);
        curl_close($ch); curl_setopt ($ ch);
        return $result; return $ resultado;
    } )
    else return $result['error']='err'; else return $ result [ 'error'] = 'ven';
} ) 

fsockopen fsockopen

 function getData($url) { function getData ($ url) (
    $arr=parse_url($url); $ Arr = parse_url ($ url);
    $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); $ Fp = fsockopen ($ arr [ 'servidor'], 80, $ errno, $ errstr, 30);
    if(!$fp) { if (! $ fp) (
        return false; return false;
    }else { ) Else (
    // send headers / / Enviar os cabezallos
        $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $ Out = "GET". Fsockopen ($ arr [ 'servidor'], 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'],'',$ host). "\ 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); $ Conteído .= fgets ($ fp, 4096);
        }; );
        fclose($fp); fclose ($ fp);
        return $contents; return $ contenido;
    } )
} ) 

file_get_contents file_get_contents

 function getData($url) { function 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. Como ve o xeito máis doado de baixar o contido da rede está a usar a función file_get_contents, pero se precisa de máis opcións, especialmente se está a traballar cos títulos, entón CURL é a mellor forma de ir a vostede.

Translate this post Traducir esta post


Related posts: Related posts:

  1. JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Enviar funcionar como un parámetro para outra función (callbacks)

  • saya kemarin juga ngerjain web yang grabb data dari website lain... saya juga Kemarin web ngerjain datos Grabber dari yang lain sitio web ...

    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 (), datos ngambil BUAT yang kan butuh di ...


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