Posted By

dereklio on 06/10/08


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Juntalis


Http Bot class in PHP


 / Published in: PHP
 

  1. <?php
  2.  
  3. require_once('HTTP/Client.php');
  4. require_once(dirname(__FILE__) . '/Shell.class.php');
  5.  
  6. class HttpBot {
  7.  
  8. private static $singleton;
  9.  
  10. public static function singleton() {
  11. if (!isset(self::$singleton)) {
  12. $c = __CLASS__;
  13. self::$singleton = new $c;
  14. }
  15.  
  16. return self::$singleton;
  17. }
  18.  
  19. private $http;
  20.  
  21. private function __construct() {
  22. $shell = Shell::singleton();
  23. $this->http = new HTTP_Client;
  24. $this->signature_purge(2);
  25.  
  26. // debug codes, enabled for debug
  27. $this->signature_purge(0);
  28. }
  29.  
  30. public function __clone() {
  31. trigger_error('Clone is not allowed.', E_USER_ERROR);
  32. }
  33.  
  34. public function signature_exists($html) {
  35. $shell = Shell::singleton();
  36. return file_exists($shell->temp(md5($html) . '.sig'));
  37. }
  38.  
  39. public function signature_save($html) {
  40. $shell = Shell::singleton();
  41. return $shell->touch_file($shell->temp(md5($html) . '.sig'));
  42. }
  43.  
  44. public function signature_purge($days_old) {
  45. $shell = Shell::singleton();
  46. if ($dh = opendir($shell->temp())) {
  47. while (($file = readdir($dh)) !== false) {
  48. $path = $shell->temp($file);
  49. if (is_file($path)) {
  50. if ((time() - filemtime($path)) / 3600 / 24 > $days_old) {
  51. if (array_pop(explode('.', $file)) == 'sig') {
  52. unlink($path);
  53. }
  54. }
  55. }
  56. }
  57. closedir($dh);
  58. }
  59. }
  60.  
  61. private function assert_response($code, $text = false) {
  62. if ($code != 200) {
  63. if ($text !== false) trigger_error($text . ' (Response Code: ' . $code . ')', E_USER_ERROR);
  64. return false;
  65. }
  66. return true;
  67. }
  68.  
  69. private function assert_text($body, $chk, $text = false) {
  70. if (strpos($body, $chk) === false) {
  71. if ($text !== false) trigger_error($text, E_USER_ERROR);
  72. return false;
  73. }
  74. return true;
  75. }
  76.  
  77. public function normalize_string($text) {
  78. $text = str_replace(array("\r", "\n", "\t"), " ", $text);
  79. while (strpos($text, ' ') !== false) {
  80. $text = str_replace(' ', ' ', $text);
  81. }
  82. return trim($text);
  83. }
  84.  
  85. public function response($encoding = false) {
  86. $shell = Shell::singleton();
  87. $resp = $this->http->currentResponse();
  88. $raw = $resp['body'];
  89.  
  90. if ($encoding !== false) {
  91. $raw = $shell->concmd_utf8($raw, $encoding);
  92. }
  93. $raw = iconv('utf-8', 'utf-8//IGNORE', $raw);
  94.  
  95. $body = mb_convert_encoding($raw, 'HTML-ENTITIES', 'utf-8');
  96.  
  97. $dom = new DOMDocument;
  98. $dom->preserveWhiteSpace = false;
  99. @$dom->loadHTML($body);
  100.  
  101. $xpath = new DOMXPath($dom);
  102.  
  103. return array($dom, $xpath, $raw);
  104. }
  105.  
  106. public function geta($url, $error = false) {
  107. return $this->assert_response($this->http->get($url), $error);
  108. }
  109.  
  110. public function posta($url, $data, $error = false) {
  111. return $this->assert_response($this->http->post($url, $data), $error);
  112. }
  113.  
  114. public function contenta($chk, $text = false) {
  115. $resp = $this->http->currentResponse();
  116. return $this->assert_text($resp['body'], $chk, $text);
  117. }
  118.  
  119. }
  120.  
  121. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Sn0opy on June 11, 2008

What does this bot do?

Posted By: dereklio on June 11, 2008

i have never thought there is someone comments on my codes, as i am just putting some of my frequently used snippets online ;)

in short, it does nothing. This class basically is a wrapper of the HTTPClient PEAR package. it just groups several functions of the HTTPClient which i used very frequently together. and it's needed to be used in conjunction of my other class "Shell.class.php" (http://snipplr.com/view/6648/a-shell-access-wrapper-for-windows-in-php/)

let's say, I want to write a script that log me into facebook and write something on someone's else wall.... I would simply do:

$http = HttpBot::singleton(); $http->geta('http://www.facebook.com', 'Unable to connect to Facebook'); if ($http->contenta('logout') === false) { // Login required, login now!

list($dom, $xpath) = $http->response();

$challenge = $dom->getElementById('challenge')->getAttribute('value');
$data = array(
    'email' => USERNAME,
    'pass' => PASSWORD,
    'challenge' => $challenge,
    'md5pass' => '1',
    'noerror' => '1',
);
$http->posta(https://login.facebook.com/login.php, $data, 'Unable to login');
$http->contenta('logout', 'Login failed');

}

i used this script to create several scheduled tasks in windows to collect / poll different contents from internet, summarize and email to me on a daily basis. it may not be a very elegant solution, but it just gives me some shorthand, while dealing with the HTTP_Client class.

You need to login to post a comment.