Http Bot class in PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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

You need to login to post a comment.