PHP PHP中的Http Bot类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP PHP中的Http Bot类相关的知识,希望对你有一定的参考价值。

<?php

require_once('HTTP/Client.php');
require_once(dirname(__FILE__) . '/Shell.class.php');

class HttpBot {

	private static $singleton;
	
	public static function singleton() {
		if (!isset(self::$singleton)) {
			$c = __CLASS__;
			self::$singleton = new $c;
		}
		
		return self::$singleton;
	}
	
	private $http;

	private function __construct() {
		$shell = Shell::singleton();
		$this->http = new HTTP_Client;
		$this->signature_purge(2);
		
		// debug codes, enabled for debug
		$this->signature_purge(0);
	}
	
	public function __clone() {
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}
	
	public function signature_exists($html) {
		$shell = Shell::singleton();
		return file_exists($shell->temp(md5($html) . '.sig'));
	}
	
	public function signature_save($html) {
		$shell = Shell::singleton();
		return $shell->touch_file($shell->temp(md5($html) . '.sig'));
	}
	
	public function signature_purge($days_old) {
		$shell = Shell::singleton();
		if ($dh = opendir($shell->temp())) {
			while (($file = readdir($dh)) !== false) {
				$path = $shell->temp($file);
				if (is_file($path)) {
					if ((time() - filemtime($path)) / 3600 / 24 > $days_old) {
						if (array_pop(explode('.', $file)) == 'sig') {
							unlink($path);
						}
					}
				}
			}
			closedir($dh);
		}
	}

	private function assert_response($code, $text = false) {
		if ($code != 200) {
			if ($text !== false) trigger_error($text . ' (Response Code: ' . $code . ')', E_USER_ERROR);
			return false;
		}
		return true;
	}
	
	private function assert_text($body, $chk, $text = false) {
		if (strpos($body, $chk) === false) {
			if ($text !== false) trigger_error($text, E_USER_ERROR);
			return false;
		}
		return true;
	}
	
	public function normalize_string($text) {
		$text = str_replace(array("\r", "\n", "\t"), " ", $text);
		while (strpos($text, '  ') !== false) {
			$text = str_replace('  ', ' ', $text);
		}
		return trim($text);
	}
	
	public function response($encoding = false) {
		$shell = Shell::singleton();
		$resp = $this->http->currentResponse();
		$raw = $resp['body'];
		
		if ($encoding !== false) {
			$raw = $shell->concmd_utf8($raw, $encoding);
		}
		$raw = iconv('utf-8', 'utf-8//IGNORE', $raw);
		
		$body = mb_convert_encoding($raw, 'HTML-ENTITIES', 'utf-8');
		
		$dom = new DOMDocument;
		$dom->preserveWhiteSpace = false;
		@$dom->loadHTML($body);
		
		$xpath = new DOMXPath($dom);
		
		return array($dom, $xpath, $raw);
	}
	
	public function geta($url, $error = false) {
		return $this->assert_response($this->http->get($url), $error);
	}
	
	public function posta($url, $data, $error = false) {
		return $this->assert_response($this->http->post($url, $data), $error);
	}

	public function contenta($chk, $text = false) {
		$resp = $this->http->currentResponse();
		return $this->assert_text($resp['body'], $chk, $text);
	}

}

?>

以上是关于PHP PHP中的Http Bot类的主要内容,如果未能解决你的问题,请参考以下文章

PHP中的API Pinterest - 登录失败

PHP PHP bot获取维基百科的定义

PHP PHP bot,用CURL检索页面文本

PHP 使用PHP检测Bot,Crawlers和Spiders(西班牙语)

php Botónpublicar粘性

PHP Google bot抓取通知程序