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 ACTUALIZACIONES VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / PHP / PHP: How to download a webpa… Inicio / codificación / PHP / PHP: Cómo descargar un webpa ...

PHP: How to download a webpage (aka web scrapping) with PHP PHP: Cómo descargar una página web (también conocido como el desguace de la web) con PHP

Posted on 03. Publicado en 03. Oct, 2009 by Dragos in Coding , PHP Octubre de 2009, por Dragos en la codificación, PHP

There are many ways of downloading web pages, or web content. Hay muchas maneras de descargar páginas web o el contenido web. Personally I like to use cURL for my web scrapping needs, but sometimes I also use fsockopen and file_get_contents . Personalmente me gusta usar CURL para mis necesidades de desguace web, pero a veces también utilizan fsockopen y file_get_contents.

Here are 3 different functions that will allow you to download web content. Aquí hay 3 diferentes funciones que le permitirá descargar contenido web.

cURL : cURL:

 function getData($url) { getData función ($ 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) 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'] = 'errar';
} ) 

fsockopen fsockopen

 function getData($url) { getData función ($ url) (
    $arr=parse_url($url); $ matriz = parse_url ($ url);
    $fp = fsockopen($arr['host'], 80, $errno, $errstr, 30); $ fp = fsockopen ($ matriz [ 'host'], 80, $ errno, $ errstr, 30);
    if(!$fp) { if ($ fp) (
        return false; return false;
    }else { ) else (
    // send headers / / Enviar las cabeceras de
        $out = "GET ".fsockopen($arr['host'], 80, $errno, $errstr, 30)." HTTP/1.1\r\n"; $ out = "GET". fsockopen ($ matriz [ 'host'], 80, $ errno, $ errstr, 30). "HTTP/1.1 \ r \ n";
        $out .= "Host: ".str_replace('http://'.$arr['host'],'',$url)."\r\n"; $ salida .= "Host:". str_replace ( 'http://'. $ matriz [ 'url'],'',$ de acogida). "\ r \ n";
        $out .= "User-Agent: FSOCKOPEN\r\n"; $ salida .= "User-Agent: fsockopen \ r \ n";
        $out .= "Connection: Close\r\n\r\n"; $ salida .= "Connection: close \ r \ n \ r \ n";
        fwrite($fp, $out); fwrite ($ fp, $ out);
        while(!feof($fp)) { while (! feof ($ fp)) (
            $contents .= fgets($fp, 4096); $ contenido .= fgets ($ fp, 4096);
        }; );
        fclose($fp); fclose ($ fp);
        return $contents; return $ contenido;
    } )
} ) 

file_get_contents file_get_contents

 function getData($url) { getData función ($ 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. Como se puede ver la forma más fácil de descargar contenido web es mediante la función file_get_contents, pero si necesita más opciones, sobre todo si se trabaja con los encabezados, a continuación, cURL es la mejor manera de ir para ti.

Translate this post Traducir este mensaje


Related posts: Puestos relacionados con:

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

  • saya kemarin juga ngerjain web yang grabb data dari website lain... saya kemarin juga ngerjain web yang datos grabb dari permanecido 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... Pake di potong2 preg_replace (), buat datos ngambil Yang di butuh kan ...


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