Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/05/2014, 12:49
botxii
 
Fecha de Ingreso: octubre-2012
Mensajes: 135
Antigüedad: 11 años, 7 meses
Puntos: 8
Clase para utilizar Curl

En mi último proyecto estoy utilizando la librería de php Curl. Así que he creado una clase sencilla para utilizar fácilmente esta librería.

Código PHP:
class conectar{
    public 
$html

    public function 
__construct($url=""
    {
        if (
$url != ""){
            
            
$this->url=$url
            
$this->ch curl_init();  
                
        }else{
            echo 
'Error: url = ""';
            exit;
        }
    }
    
        public function 
tiempo($max
    {
        if(isset(
$max)){
          
            
curl_setopt($this->chCURLOPT_CONNECTTIMEOUT$max);
        }else{
            echo 
'Error: tiempo maximo = ""';
            exit;
    
        }
    }
     
    
    public function 
obtener_html() 
    {
      

        
curl_setopt($this->chCURLOPT_RETURNTRANSFERtrue); 
        
curl_setopt($this->chCURLOPT_URL$this->url); 
        
$this->html curl_exec($this->ch);  
        
curl_close($this->ch);

        return 
$this->html;
    
    
  }
     
      public function 
anadir_get($array
    {
        if(
count($array) == 0){
            echo 
'Error: array_get = ""';
            exit;
        }else{
            
$this->get "?";
            foreach ( 
$array as $clave=>$valor){
            
            
                
$this->get .=$clave."=".$valor."&";
            
            
            }
            
$this->url .=$this->get;
        }
    
    }
  
  
        public function 
redir($max=2)
    {
        
curl_setopt($this->chCURLOPT_RETURNTRANSFERtrue);
        
curl_setopt($this->chCURLOPT_MAXREDIRS,$max);
    
    }
  
  
      public function 
anadir_post($array
    {
        if(
count($array) == 0){
            echo 
'Error: array_post = ""';
            exit;
        }else{
             
curl_setopt($this->chCURLOPT_POSTtrue);  

            
curl_setopt($this->chCURLOPT_POSTFIELDShttp_build_query($array)); 
        
        }

    
    }
  
  
      public function 
anadir_cookie($array
    {
        
        if(
is_array($array) and count($array) != 0){
         
            
$this->cookie "";
            
$cont 1;
            foreach ( 
$array as $clave=>$valor){
                    
                    if(
$cont count($array)){
                        
$this->cookie .=$clave."=".$valor.";";
                    }else{
                        
$this->cookie .=$clave."=".$valor;
                    }
                
$cont++;
            }

        }else{
                echo 
'Error: array_cookie = ""';
            exit;
        }
    
        
curl_setopt($this->ch,CURLOPT_COOKIE,$this->cookie);
        


    }

Se utiliza de manera fácil.

Código PHP:
$conexion = new conectar("url"); // iniciamos la clase
$conexion->anadir_post($array); // array con las variables que queremos mandar por post
$conexion->anadir_get($array);// array con las variables que queremos mandar por get
$conexion->anadir_cookie($array);// array con las variables que queremos mandar por cookie
$conexion->redir(2); // redireccionar y número max de ellas.(2 por defecto)
$conexion->tiempo(30); //tiempo máximo.

$html $conexion->obtener_html();  // obtienes el código fuente de la url 

Si la valoráis y la veis útil, seguiré con ella, y la mejoraré.

Saludos.