• Martes 16 de Abril de 2024, 12:26

Autor Tema:  [Reto] Bot http  (Leído 1515 veces)

Invisible

  • Nuevo Miembro
  • *
  • Mensajes: 7
    • Ver Perfil
[Reto] Bot http
« en: Domingo 25 de Diciembre de 2011, 14:52 »
0
Hay un tipo de retos incrementales que son más productivos que los retos habituales.

http://en.wikipedia.org/wiki/Cutting_contest
http://en.wikipedia.org/wiki/Guitar_battle

Esto es un reto a ese estilo, consiste en ir añadiendo algo al bot para hacerlo más parecido a una persona en la navegación (con cookies, con referer, con todo eso), además para que sea más fácil indicarle los parámetros que tiene que rellenar (por ejemplo muchos formularios tienen campos hidden, podría rellenarlos él mismo), para que sea más fácil parsear el html después, para que sea más fácil usar proxies... el límite lo pone vuestra imaginación.

Creo que puede ser un reto interesante, entretenido, productivo y del que todos saquemos el divertirnos, un código interesante y aprender y hacer algo útil. Vosotros diréis.

Aquí tenéis un ejemplo de cómo podría empezarse, veremos si alguien continua o no, en unos días pondré una mejora si nadie se anima, y con eso espero que ya sí se anime alguien.

Código: PHP
  1. class PHPBot {
  2.  
  3.     function PHPBot (){
  4.       $this->ch = curl_init();
  5.       $ch = $this->ch;
  6.       curl_setopt($ch, CURLOPT_HEADER, 1); // Include headers in response or not
  7.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return (don't print) answer of exec
  8.       curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.12) Gecko/2009070611 Firefox/3.0.12");
  9.       curl_setopt($ch, CURLOPT_AUTOREFERER, true); // Isn't this great?
  10.       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: */*', 'Accept-Language: en-us,en;q=0.5', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'));      
  11.     }
  12.    
  13.     private function act($url, $params = false){
  14.       $ch = $this->ch;
  15.       curl_setopt($ch, CURLOPT_URL, $url);
  16.       if ($params != false){
  17.         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->myurlencode($params));
  18.         curl_setopt($ch, CURLOPT_POST, 1);
  19.       }
  20.       else
  21.         curl_setopt($ch, CURLOPT_POST, 0);
  22.       $r = curl_exec($ch);
  23.       return $r;
  24.     }
  25.    
  26.     private function myurlencode($dict){
  27.       $r = "";
  28.       foreach($dict as $key => $value)
  29.         $r = $r.urlencode($key).'='.urlencode($value).'&';
  30.       return $r;
  31.     }
  32.    
  33.     function get ($url){
  34.       return $this->act($url);
  35.     }
  36.    
  37.     function post($url, $params){
  38.       return $this->act($url, $params);
  39.     }
  40.  
  41.   }