Ver Mensaje Individual
  #4 (permalink)  
Antiguo 19/05/2014, 13:52
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: Clase para utilizar Curl

Excelente... no lo he probado.. pero te hice unos cambios:

Código PHP:
Ver original
  1. <?php
  2. /*
  3.     EasyCurl
  4.     @author: Cesar Blanco Guillamon (16)   
  5.     @history: cambio de nombre a la clase, cambio de nombre a metodos, cambio de visibilidad de metodos, uso de excepciones
  6.  
  7. */
  8. class EasyCurl{
  9.     private $html;  
  10.     private $url;  
  11.     private $ch;  
  12.     private $http_get;
  13.  
  14.     public function __construct($url)  
  15.     {
  16.         if (!empty($url)){
  17.              
  18.             $this->url=$url;  
  19.             $this->ch = curl_init();               
  20.                  
  21.         }else
  22.             throw new Exception('Error: url = ""');        
  23.     }
  24.      
  25.     public function setTiempo($max)  
  26.     {
  27.         if(isset($max))                
  28.             curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $max);
  29.         else
  30.             throw new Exception('Error: tiempo maximo = ""');            
  31.          
  32.     }
  33.      
  34.      
  35.     public function getHtml()  
  36.     {      
  37.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);  
  38.         curl_setopt($this->ch, CURLOPT_URL, $this->url);  
  39.         $this->html = curl_exec($this->ch);  
  40.         curl_close($this->ch);
  41.  
  42.         return $this->html;
  43.     }
  44.      
  45.      public function addGet($array)  
  46.     {
  47.         if(count($array) == 0)
  48.             throw new Exception('Error: array_get = ""');                      
  49.         else{
  50.             $this->http_get = "?";
  51.             foreach ( $array as $clave=>$valor){
  52.              
  53.              
  54.                 $this->http_get .=$clave."=".$valor."&";
  55.              
  56.              
  57.             }
  58.             $this->url .=$this->http_get;
  59.         }
  60.      
  61.     }
  62.    
  63.        public function redir($max=2)
  64.     {
  65.         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  66.         curl_setopt($this->ch, CURLOPT_MAXREDIRS,$max);
  67.      
  68.     }
  69.    
  70.    
  71.     public function addPost($array)  
  72.     {
  73.         if(count($array) == 0)
  74.             throw new Exception('Error: array_post = ""');            
  75.         else{
  76.             curl_setopt($this->ch, CURLOPT_POST, true);  
  77.             curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($array));          
  78.         }
  79.  
  80.      
  81.     }
  82.    
  83.    
  84.     public function addCookie($array)  
  85.     {
  86.          
  87.         if(is_array($array) and count($array) != 0){
  88.          
  89.             $this->cookie = "";
  90.             $cont = 1;
  91.             foreach ( $array as $clave=>$valor){
  92.                      
  93.                     if($cont < count($array)){
  94.                         $this->cookie .=$clave."=".$valor.";";
  95.                     }else{
  96.                         $this->cookie .=$clave."=".$valor;
  97.                     }
  98.                 $cont++;
  99.             }
  100.  
  101.         }else{
  102.                 throw new Exception('Error: array_cookie = ""');
  103.             exit;
  104.         }
  105.      
  106.         curl_setopt($this->ch,CURLOPT_COOKIE,$this->cookie);
  107.          
  108.  
  109.  
  110.     }
  111. }  
  112.  
  113.  
  114. // Prueba:
  115.  
  116. $conexion = new EasyCurl("https://www.facebook.com/meryttt"); // iniciamos la clase
  117. exit();
  118. $conexion->addPost($array); // array con las variables que queremos mandar por post
  119. $conexion->addGet($array);// array con las variables que queremos mandar por get
  120. $conexion->addCookie($array);// array con las variables que queremos mandar por cookie
  121. $conexion->redir(2); // redireccionar y número max de ellas.(2 por defecto)
  122. $conexion->setTime(30); //tiempo máximo.
  123.  
  124. $html = $conexion->obtener_html();  // obtienes el código fuente de la url
__________________
Salu2!

Última edición por Italico76; 19/05/2014 a las 14:08