Autor Tema: Clase Wget  (Leído 4069 veces)

Desconectado alpha

  • PHPerit@
  • *
  • Mensajes: 5
  • Karma: 0
  • Sexo: Masculino
    • Ver Perfil
    • Codificando
Clase Wget
« en: 19 de Septiembre de 2014, 06:02:18 am »
Hola, he creado una clase para leer páginas web, bajarse archivos... la he utilizado para bajarme los paquetes de instalación de PHP y los de las extensiones PECL que suelo utilizar, por ejemplo.

Por supuesto su utilidad depende de la imaginación y necesidades de cada uno.

La historia y el código lo podéis leer en http://alphaonphp.blogspot.com.es/p/clase-wget.html

Para el que no quiera entretenerse con historias aquí está el meollo (también adjunto):

Código: [Seleccionar]
<?php

  
if (!defined(&#39;STDOUT&#39;)) {
    
define(&#39;STDOUT&#39;, fopen(&#39;php://stdout&#39;, &#39;w&#39;));
  
}

  if (!
function_exists(&#39;curl_strerror&#39;)) {
    
function curl_strerror ($curl_error) {
      return 
$curl_error;
    }
  }

  if (!
function_exists(&#39;simplexml_import_html&#39;)) {
    
function simplexml_import_html ($html) {
      
$doc = new DOMDocument();
      @
$doc->loadHTML($html);
      
$xml simplexml_import_dom($doc);
      return 
$xml;
    }
  }

  class 
wget {
    protected 
$curl null;
    protected 
$cookie_jar = &#39;&#39;;

    
public function __construct ($useragent null$cookie_jar = &#39;&#39;) {
      
$this->curl curl_init();
      
$this->cookie_jar $cookie_jar;
      if (isset(
$useragent)) curl_setopt($this->curlCURLOPT_USERAGENT$useragent);
      
curl_setopt($this->curlCURLOPT_ENCODING, &#39;&#39;);
      
curl_setopt($this->curlCURLOPT_PROGRESSFUNCTION, array($this, &#39;progress&#39;));
      
curl_setopt($this->curlCURLOPT_FILETIMEtrue);
      
curl_setopt($this->curlCURLOPT_COOKIESESSIONtrue);
      
curl_setopt($this->curlCURLOPT_COOKIEFILE$this->cookie_jar);
      
curl_setopt($this->curlCURLOPT_COOKIEJAR$this->cookie_jar);
    }

    public function 
__destruct () {
      
curl_close($this->curl);
    }

    protected function 
progress () {
      static 
$url null;
      static 
$progress_bar null;

      
$curl_info curl_getinfo($this->curl);
      if (
$url == $curl_info[&#39;url&#39;]) echo str_repeat("\x08", strlen($progress_bar));

      
$url $curl_info[&#39;url&#39;];
      
$progress_bar null;
      if (
$curl_info[&#39;http_code&#39;] == 200) {
        
$down_perc 0;
        if (
$curl_info[&#39;size_download&#39;] <= $curl_info[&#39;download_content_length&#39;]) $down_perc = (100 * $curl_info[&#39;size_download&#39;]) / $curl_info[&#39;download_content_length&#39;];
        
$eta = &#39;N/A&#39;;
        
if ($curl_info[&#39;speed_download&#39;]) $eta = ($curl_info[&#39;download_content_length&#39;] - $curl_info[&#39;size_download&#39;]) / $curl_info[&#39;speed_download&#39;];
        
$progress_bar sprintf(&#39;[%-30s] %3d%% %02.3fs %02.3fs&#39;, str_repeat(&#39;#&#39;, 30 * $down_perc / 100), $down_perc, $eta, $curl_info[&#39;total_time&#39;]);
      
}
      echo 
$progress_bar"\x20\x08";
    }

    public function 
set_auth ($user null$pass null$auth null) {
      if (empty(
$auth)) $auth CURLAUTH_ANY;
      
curl_setopt($this->curlCURLOPT_HTTPAUTH$auth);
      
curl_setopt($this->curlCURLOPT_USERPWD$user . &#39;:&#39; . $pass);
    
}

    public function 
set_ssl_verifypeer ($ssl_verifypeer false) {
      
curl_setopt($this->curlCURLOPT_SSL_VERIFYPEER$ssl_verifypeer);
    }

    public function 
set_verbose ($verbose true) {
      
curl_setopt($this->curlCURLOPT_VERBOSE, (bool) $verbose);
    }

    public function 
set_followlocation ($follow true) {
      
curl_setopt($this->curlCURLOPT_FOLLOWLOCATION, (bool) $follow);
    }

    public function 
set_maxredirs ($maxredirs 0) {
      
curl_setopt($this->curlCURLOPT_MAXREDIRS, (int) $maxredirs);
    }

    public function 
set_header_out ($header_out true) {
      
curl_setopt($this->curlCURLINFO_HEADER_OUT, (bool) $header_out);
    }

    public function 
set_header ($header null) {
      
curl_setopt($this->curlCURLOPT_HTTPHEADER$header);
    }

    public function 
info_url ($url$referer null) {
      
curl_setopt($this->curlCURLOPT_HTTPGETtrue);
      
curl_setopt($this->curlCURLOPT_URL$url);
      
curl_setopt($this->curlCURLOPT_REFERER$referer);
      
curl_setopt($this->curlCURLOPT_NOPROGRESStrue);
      
curl_setopt($this->curlCURLOPT_NOBODYtrue);
      
curl_setopt($this->curlCURLOPT_FILESTDOUT);
      
curl_setopt($this->curlCURLOPT_RETURNTRANSFERtrue);
      
$body curl_exec($this->curl);
      
$curl_info curl_getinfo($this->curl);
      
$curl_info[&#39;body&#39;] = $body;
      
$curl_info[&#39;curl_errno&#39;] = curl_errno($this->curl);
      
$curl_info[&#39;curl_strerror&#39;] = curl_strerror($curl_info[&#39;curl_errno&#39;]);
      
return $curl_info;
    }

    public function 
get_url ($url$referer null$post_fields null) {
      
curl_setopt($this->curlCURLOPT_POSTis_array($post_fields));
      
curl_setopt($this->curlCURLOPT_POSTFIELDS$post_fields);
      
curl_setopt($this->curlCURLOPT_HTTPGET, !is_array($post_fields));
      
curl_setopt($this->curlCURLOPT_URL$url);
      
curl_setopt($this->curlCURLOPT_REFERER$referer);
      
curl_setopt($this->curlCURLOPT_NOPROGRESStrue);
      
curl_setopt($this->curlCURLOPT_NOBODYfalse);
      
curl_setopt($this->curlCURLOPT_FILESTDOUT);
      
curl_setopt($this->curlCURLOPT_RETURNTRANSFERtrue);
      
$body curl_exec($this->curl);
      
$curl_info curl_getinfo($this->curl);
      
$curl_info[&#39;body&#39;] = $body;
      
$curl_info[&#39;curl_errno&#39;] = curl_errno($this->curl);
      
$curl_info[&#39;curl_strerror&#39;] = curl_strerror($curl_info[&#39;curl_errno&#39;]);
      
return $curl_info;
    }

    public function 
get_file ($url$filename null$referer null$progress true) {
      if (empty(
$filename)) $filename parse_url($urlPHP_URL_HOST) . parse_url($urlPHP_URL_PATH);
      if (!
is_dir(dirname($filename))) mkdir(dirname($filename), 0775true);

      
$file_handle fopen($filename, &#39;w&#39;);

      
curl_setopt($this->curlCURLOPT_HTTPGETtrue);
      
curl_setopt($this->curlCURLOPT_URL$url);
      
curl_setopt($this->curlCURLOPT_REFERER$referer);
      
curl_setopt($this->curlCURLOPT_NOPROGRESS, (!$progress));
      
curl_setopt($this->curlCURLOPT_NOBODYfalse);
      
curl_setopt($this->curlCURLOPT_RETURNTRANSFERfalse);
      
curl_setopt($this->curlCURLOPT_FILE$file_handle);
      
curl_exec($this->curl);

      
fclose($file_handle);

      
$curl_info curl_getinfo($this->curl);
      
touch($filename$curl_info[&#39;filetime&#39;]);

      
$curl_info[&#39;filename&#39;] = $filename;
      
$curl_info[&#39;curl_errno&#39;] = curl_errno($this->curl);
      
$curl_info[&#39;curl_strerror&#39;] = curl_strerror($curl_info[&#39;curl_errno&#39;]);
      
return $curl_info;
    }
  }
?>
Programar es una pasión, resolver problemas un pasatiempo, superarse una meta.

Comunidad PHPeros

Clase Wget
« en: 19 de Septiembre de 2014, 06:02:18 am »