├── application ├── views │ ├── error │ │ └── error.phtml │ └── index │ │ └── index.phtml ├── library │ ├── Common │ │ ├── Util.php │ │ ├── Logger │ │ │ └── Helper.php │ │ ├── Template.php │ │ └── Request │ │ │ └── Helper.php │ └── Db │ │ ├── Factory.php │ │ ├── DbInterface.php │ │ └── Mysql.php ├── models │ ├── BaseModel.php │ └── IndexModel.php ├── controllers │ ├── Index.php │ ├── Error.php │ └── Base.php └── Bootstrap.php ├── log ├── yaf.log └── Yaf_Log ├── conf ├── application.ini └── auto.php ├── README.md └── index.php /application/views/error/error.phtml: -------------------------------------------------------------------------------- 1 | 2 | error occured 3 | -------------------------------------------------------------------------------- /log/yaf.log: -------------------------------------------------------------------------------- 1 | [2012-05-24 01:12:41]516--Could not find controller script /var/www/ata/application/controllers/Sdfsdf.php 2 | -------------------------------------------------------------------------------- /application/views/index/index.phtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Hello World 7 | 8 | 9 | Hellow World! 10 | 11 | 12 | -------------------------------------------------------------------------------- /application/library/Common/Util.php: -------------------------------------------------------------------------------- 1 | db = Factory::create(); 25 | } 26 | } -------------------------------------------------------------------------------- /application/models/IndexModel.php: -------------------------------------------------------------------------------- 1 | showPage(); 17 | } 18 | 19 | public function showHeader() { 20 | echo '重载过的头部'; 21 | $this->tpl->set('exp', 'exp text'); 22 | $this->showTpl('index/index.phtml'); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /log/Yaf_Log: -------------------------------------------------------------------------------- 1 | 2012-05-24 12:20:08 /var/www/ata/application/controllers/Index.php 11 IndexController::indexAction debug 2 | 2012-05-24 12:20:22 /var/www/ata/application/controllers/Index.php 11 IndexController::indexAction debug 3 | 2012-05-24 12:20:27 /var/www/ata/application/controllers/Index.php 11 IndexController::indexAction debug 4 | 2012-05-24 12:20:28 /var/www/ata/application/controllers/Index.php 11 IndexController::indexAction debug 5 | 2012-05-24 12:20:29 /var/www/ata/application/controllers/Index.php 11 IndexController::indexAction debug 6 | -------------------------------------------------------------------------------- /application/controllers/Error.php: -------------------------------------------------------------------------------- 1 | getException()获取到发生的异常 12 | */ 13 | public function errorAction($exception) { 14 | assert($exception === $exception->getCode()); 15 | $this->tpl->set("code", $exception->getCode()); 16 | $this->tpl->set("message", $exception->getMessage()); 17 | $this->showTpl( 'error/error.phtml' ); 18 | } 19 | } -------------------------------------------------------------------------------- /conf/application.ini: -------------------------------------------------------------------------------- 1 | [common] 2 | application.directory=APP_PATH "/application/" 3 | application.tpl.path = APP_PATH "/application/views/" 4 | 5 | [testing : common] 6 | site.webhost = yaf.taobao.ali.com 7 | 8 | 9 | ;#######################每个数据库的配置文件必须有下列6个字段########################## 10 | ;##添加格式 11 | ;#db.$name.type = 数据库类型 12 | ;#db.$name.host = 数据库host 13 | ;#db.$name.usr = 数据库账号 14 | ;#db.$name.pwd = 数据库密码 15 | ;#db.$name.dbname = 数据库名称 16 | ;#db.$name.charset = 编码格式 17 | ;################################################################################# 18 | 19 | 20 | -------------------------------------------------------------------------------- /application/library/Db/Factory.php: -------------------------------------------------------------------------------- 1 | db->$which; 21 | 22 | $class_name = '\Db\\' . ucfirst(strtolower( $db_config->type ) ); 23 | 24 | $db = $class_name::getInstance( $db_config ); 25 | 26 | return ($db instanceof DbInterface) ? $db : false; 27 | 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /application/library/Db/DbInterface.php: -------------------------------------------------------------------------------- 1 | You need to successfully install yaf. 12 | download-url: http://pecl.php.net/package/yaf 13 | Official Website: http://yaf.laruence.com/ 14 | 15 | 16 | 2> By adding the following under the configuration section in the configuration file (.INI) 17 | 18 | yaf.environ=testing 19 | yaf.cache_config=0 20 | yaf.use_spl_autoload=1 21 | yaf.use_namespace=1 22 | 23 | Important : yaf.environ best according to different environments and in php.ini 24 | 25 | 3> Open rewrite module (check it) 26 | 27 | Feel free to contact me if you have any questions. 28 | 29 | Email:zxcvdavid@gmail.com 30 | 31 | Thx. 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /application/controllers/Base.php: -------------------------------------------------------------------------------- 1 | application->tpl->path; 22 | 23 | $this->tpl = new Template ( $tpl_path ); 24 | 25 | \Yaf\Dispatcher::getInstance()->disableView(); 26 | } 27 | 28 | public function showPage(){ 29 | $this->showHeader(); 30 | $this->showLeft(); 31 | $this->showRight(); 32 | $this->showFoot(); 33 | } 34 | 35 | public function showHeader(){ 36 | echo '输出头部'; 37 | } 38 | 39 | public function showLeft(){ 40 | echo '输出左侧'; 41 | } 42 | 43 | public function showRight(){ 44 | echo '输出右侧'; 45 | } 46 | 47 | public function showFoot(){ 48 | echo '输出底部'; 49 | } 50 | 51 | public function showTpl( $path = '' ){ 52 | echo $this->tpl->fetch($path); 53 | $this->tpl = null; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /application/Bootstrap.php: -------------------------------------------------------------------------------- 1 | start(); 18 | 19 | header('content-type:text/html;charset=utf-8'); 20 | } 21 | 22 | public function _initConfig() { 23 | $config = \Yaf\Application::app()->getConfig(); 24 | \Yaf\Registry::set("config", $config); 25 | } 26 | 27 | public function _initLogger(\Yaf\Dispatcher $dispatcher){ 28 | \Common\Logger\Helper::$_logpath = APP_PATH . '/log'; 29 | } 30 | 31 | public function _initDefaultName(\Yaf\Dispatcher $dispatcher) { 32 | $dispatcher->setDefaultModule("Index")->setDefaultController("Index")->setDefaultAction("index"); 33 | } 34 | 35 | public function _initDb(\Yaf\Dispatcher $dispatcher){ 36 | \Db\Factory::create(); 37 | } 38 | 39 | public function _initRoute(\Yaf\Dispatcher $dispatcher) { 40 | $router = \Yaf\Dispatcher::getInstance()->getRouter(); 41 | //创建一个路由协议实例 42 | $route = new \Yaf\Route\Rewrite( 43 | 'exp/:ident', 44 | array( 45 | 'controller' => 'index', 46 | 'action' => 'index' 47 | ) 48 | ); 49 | //使用路由器装载路由协议 50 | $router->addRoute('exp', $route); 51 | } 52 | 53 | public function _initUtil(\Yaf\Dispatcher $dispatcher){ 54 | \Yaf\Loader::import('Common/Util.php'); 55 | } 56 | } -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | environ() ){ 56 | case 'testing': 57 | default : 58 | error_reporting(E_ALL); 59 | ini_set('display_errors', 'On'); 60 | break; 61 | 62 | case 'production': 63 | error_reporting(0); 64 | ini_set('display_errors', 'Off'); 65 | break; 66 | } 67 | $app->bootstrap()->run(); 68 | 69 | echo ''; 70 | 71 | } catch (Exception $e){ 72 | file_put_contents('log/yaf.log',"[" . date("Y-m-d H:i:s", time()) . "]". $e->getCode() . '--' . $e->getMessage() . "\r\n" , FILE_APPEND); 73 | header('location:/'); 74 | } 75 | 76 | -------------------------------------------------------------------------------- /application/library/Common/Logger/Helper.php: -------------------------------------------------------------------------------- 1 | 75 | self::$_log_size) { 76 | self::$_logfile = self::$_logfilename . ++ self::$_filenum; 77 | } 78 | 79 | 80 | file_put_contents(self::$_logpath . '/' . self::$_logfile,$logmsg, FILE_APPEND); 81 | 82 | } 83 | } -------------------------------------------------------------------------------- /application/library/Common/Template.php: -------------------------------------------------------------------------------- 1 | path = $path; 45 | $this->vars = array(); 46 | } 47 | 48 | 49 | /** 50 | * Set the path to the template files. 51 | * 52 | * @param string $path path to template files 53 | * 54 | * @return void 55 | */ 56 | function set_path($path) { 57 | $this->path = $path; 58 | } 59 | 60 | /** 61 | * Set a template variable. 62 | * 63 | * @param string $name name of the variable to set 64 | * @param mixed $value the value of the variable 65 | * 66 | * @return void 67 | */ 68 | function set($name, $value) { 69 | $this->vars[$name] = $value; 70 | } 71 | 72 | /** 73 | * Set a bunch of variables at once using an associative array. 74 | * 75 | * @param array $vars array of vars to set 76 | * @param bool $clear whether to completely overwrite the existing vars 77 | * 78 | * @return void 79 | */ 80 | function set_vars($vars, $clear = false) { 81 | if($clear) { 82 | $this->vars = $vars; 83 | } 84 | else { 85 | if(is_array($vars)) $this->vars = array_merge($this->vars, $vars); 86 | } 87 | } 88 | 89 | /** 90 | * Open, parse, and return the template file. 91 | * 92 | * @param string string the template file name 93 | * 94 | * @return string 95 | */ 96 | function fetch($file) { 97 | extract($this->vars); // Extract the vars to local namespace 98 | ob_start(); // Start output buffering 99 | include($this->path . $file); // Include the file 100 | $contents = ob_get_contents(); // Get the contents of the buffer 101 | ob_end_clean(); // End buffering and discard 102 | return $contents; // Return the contents 103 | } 104 | 105 | function display($file) { 106 | extract($this->vars); 107 | include($this->path . $file); 108 | } 109 | 110 | } 111 | 112 | ?> 113 | -------------------------------------------------------------------------------- /application/library/Db/Mysql.php: -------------------------------------------------------------------------------- 1 | _dbh = new \PDO('mysql:dbname=' . $dbname . ';host=' . $dbhost, $username, $password, array(\PDO::ATTR_PERSISTENT => true)); 24 | $this->_dbh->query('SET NAMES ' . $dbcharset); 25 | } catch (PDOException $e) { 26 | echo '
';
 27 |             echo 'Connection failed: ' . $e->getMessage();
 28 |             die();
 29 |         }
 30 |     }
 31 | 
 32 |     static public function getInstance($db_config = '') {
 33 | 
 34 |         $_db_host = $db_config->host;
 35 |         $_db_name = $db_config->dbname;
 36 |         $_db_charset = $db_config->charset;
 37 |         $_db_usr = $db_config->usr;
 38 |         $_db_pwd = $db_config->pwd;
 39 | 
 40 |         $idx = md5($_db_host . $_db_name);
 41 | 
 42 |         if (!isset(self::$_instances[$idx])) {
 43 |             self::$_instances[$idx] = new Mysql($_db_host, $_db_usr, $_db_pwd, $_db_name, $_db_charset);
 44 |         }
 45 |         return self::$_instances[$idx];
 46 |     }
 47 | 
 48 |     function halt($msg = '', $sql = '') {
 49 | 
 50 |         $error_info = $this->_sth->errorInfo();
 51 |         $s = '
';
 52 |         $s .= 'Error:' . $error_info[2] . '
'; 53 | $s .= 'Errno:' . $error_info[1] . '
'; 54 | $s .= 'Sql:' . $this->_sql; 55 | exit($s); 56 | } 57 | 58 | function execute($sql, $values = array()) { 59 | $this->_sql = $sql; 60 | $this->_sth = $this->_dbh->prepare($sql); 61 | $bool = $this->_sth->execute($values); 62 | 63 | if ('00000' !== $this->_sth->errorCode()) { 64 | $this->halt(); 65 | } 66 | 67 | return $bool; 68 | } 69 | 70 | function getAll($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) { 71 | $this->execute($sql, $values); 72 | return $this->_sth->fetchAll($fetch_style); 73 | } 74 | 75 | function getCol($sql, $params = array(), $column_number = 0) { 76 | $columns = array(); 77 | $results = array(); 78 | $this->execute($sql, $params); 79 | $results = $this->_sth->fetchAll(\PDO::FETCH_NUM); 80 | foreach ($results as $result) { 81 | $columns[] = $result[$column_number]; 82 | } 83 | return $columns; 84 | } 85 | 86 | function getRow($sql, $values = array(), $fetch_style = \PDO::FETCH_ASSOC) { 87 | $this->execute($sql, $values); 88 | return $this->_sth->fetch($fetch_style); 89 | } 90 | 91 | function getOne($sql, $values = array(), $column_number = 0) { 92 | $this->execute($sql, $values); 93 | return $this->_sth->fetchColumn($column_number); 94 | } 95 | 96 | function insert($table = null, $data = null, $returnStr = false) { 97 | 98 | $fields = array_keys($data); 99 | $marks = array_fill(0, count($fields), '?'); 100 | 101 | $sql = "INSERT INTO $table (`" . implode('`,`', $fields) . "`) VALUES (" . implode(", ", $marks) . " )"; 102 | if ($returnStr) { 103 | $fields = array_keys($data); 104 | $marks = array_values($data); 105 | 106 | foreach ($marks as $k => $v) { 107 | if (!is_numeric($v)) 108 | $marks[$k] = '\'' . $v . '\''; 109 | } 110 | $sql = "INSERT INTO $table (`" . implode('`,`', $fields) . "`) VALUES (" . implode(", ", $marks) . " )"; 111 | return $sql; 112 | } 113 | $this->execute($sql, array_values($data)); 114 | $last_insert_id = $this->_dbh->lastInsertId(); 115 | if ($last_insert_id) 116 | return $last_insert_id; 117 | else 118 | return true; 119 | } 120 | 121 | /* 122 | * 处理事务 123 | */ 124 | 125 | function transaction($sql) { 126 | try { 127 | $this->_dbh->beginTransaction(); 128 | $this->_dbh->exec($sql); 129 | $this->_dbh->commit(); 130 | } catch (PDOException $ex) { 131 | $this->_dbh->rollBack(); 132 | } 133 | } 134 | 135 | function update($table, $data, $where) { 136 | 137 | $values = $bits = $wheres = array(); 138 | foreach ($data as $k => $v) { 139 | $bits[] = "`$k` = ?"; 140 | $values[] = $v; 141 | } 142 | 143 | foreach ($where as $c => $v) { 144 | $wheres[] = "$c = ?"; 145 | $values[] = $v; 146 | } 147 | 148 | $sql = "UPDATE $table SET " . implode(', ', $bits) . ' WHERE ' . implode(' AND ', $wheres); 149 | return $this->execute($sql, $values); 150 | } 151 | 152 | function delete($table, $where) { 153 | $values = $wheres = array(); 154 | foreach ($where as $key => $val) { 155 | $wheres[] = "$key = ?"; 156 | $values[] = $val; 157 | } 158 | 159 | $sql = "DELETE FROM $table WHERE " . implode(' AND ', $wheres); 160 | return $this->execute($sql, $values); 161 | } 162 | 163 | function close() { 164 | unset($this->_instances); 165 | unset($this->_dbh); 166 | } 167 | 168 | } -------------------------------------------------------------------------------- /application/library/Common/Request/Helper.php: -------------------------------------------------------------------------------- 1 | cookies array ready for the next request 30 | // Note: This currently ignores the cookie path (and time) completely. Time is not important, 31 | // but path could possibly lead to security problems. 32 | var $persist_referers = true; // For each request, sends path of last request as referer 33 | var $debug = false; 34 | var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found 35 | var $max_redirects = 5; 36 | var $headers_only = false; // If true, stops receiving once headers have been read. 37 | // Basic authorization variables 38 | var $username; 39 | var $password; 40 | // Response vars 41 | var $status; 42 | var $headers = array(); 43 | var $content = ''; 44 | var $errormsg; 45 | // Tracker variables 46 | var $redirect_count = 0; 47 | var $cookie_host = ''; 48 | 49 | function __construct($host, $port = 80) { 50 | $this->host = $host; 51 | $this->port = $port; 52 | } 53 | 54 | function get($path, $data = false) { 55 | $this->path = $path; 56 | $this->method = 'GET'; 57 | if ($data) { 58 | $this->path .= '?' . $this->buildQueryString($data); 59 | } 60 | return $this->doRequest(); 61 | } 62 | 63 | function post($path, $data) { 64 | $this->path = $path; 65 | $this->method = 'POST'; 66 | $this->postdata = $this->buildQueryString($data); 67 | return $this->doRequest(); 68 | } 69 | 70 | function buildQueryString($data) { 71 | $querystring = ''; 72 | if (is_array($data)) { 73 | // Change data in to postable data 74 | foreach ($data as $key => $val) { 75 | if (is_array($val)) { 76 | foreach ($val as $val2) { 77 | $querystring .= urlencode($key) . '=' . urlencode($val2) . '&'; 78 | } 79 | } else { 80 | $querystring .= urlencode($key) . '=' . urlencode($val) . '&'; 81 | } 82 | } 83 | $querystring = substr($querystring, 0, -1); // Eliminate unnecessary & 84 | } else { 85 | $querystring = $data; 86 | } 87 | return $querystring; 88 | } 89 | 90 | function doRequest() { 91 | // Performs the actual HTTP request, returning true or false depending on outcome 92 | if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) { 93 | // Set error message 94 | switch ($errno) { 95 | case -3: 96 | $this->errormsg = 'Socket creation failed (-3)'; 97 | case -4: 98 | $this->errormsg = 'DNS lookup failure (-4)'; 99 | case -5: 100 | $this->errormsg = 'Connection refused or timed out (-5)'; 101 | default: 102 | $this->errormsg = 'Connection failed (' . $errno . ')'; 103 | $this->errormsg .= ' ' . $errstr; 104 | $this->debug($this->errormsg); 105 | } 106 | return false; 107 | } 108 | socket_set_timeout($fp, $this->timeout); 109 | $request = $this->buildRequest(); 110 | $this->debug('Request', $request); 111 | fwrite($fp, $request); 112 | // Reset all the variables that should not persist between requests 113 | $this->headers = array(); 114 | $this->content = ''; 115 | $this->errormsg = ''; 116 | // Set a couple of flags 117 | $inHeaders = true; 118 | $atStart = true; 119 | // Now start reading back the response 120 | while (!feof($fp)) { 121 | $line = fgets($fp, 4096); 122 | if ($atStart) { 123 | // Deal with first line of returned data 124 | $atStart = false; 125 | if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) { 126 | $this->errormsg = "Status code line invalid: " . htmlentities($line); 127 | $this->debug($this->errormsg); 128 | return false; 129 | } 130 | $http_version = $m[1]; // not used 131 | $this->status = $m[2]; 132 | $status_string = $m[3]; // not used 133 | $this->debug(trim($line)); 134 | continue; 135 | } 136 | if ($inHeaders) { 137 | if (trim($line) == '') { 138 | $inHeaders = false; 139 | $this->debug('Received Headers', $this->headers); 140 | if ($this->headers_only) { 141 | break; // Skip the rest of the input 142 | } 143 | continue; 144 | } 145 | if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) { 146 | // Skip to the next header 147 | continue; 148 | } 149 | $key = strtolower(trim($m[1])); 150 | $val = trim($m[2]); 151 | // Deal with the possibility of multiple headers of same name 152 | if (isset($this->headers[$key])) { 153 | if (is_array($this->headers[$key])) { 154 | $this->headers[$key][] = $val; 155 | } else { 156 | $this->headers[$key] = array($this->headers[$key], $val); 157 | } 158 | } else { 159 | $this->headers[$key] = $val; 160 | } 161 | continue; 162 | } 163 | // We're not in the headers, so append the line to the contents 164 | $this->content .= $line; 165 | } 166 | fclose($fp); 167 | // If data is compressed, uncompress it 168 | if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') { 169 | $this->debug('Content is gzip encoded, unzipping it'); 170 | $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php 171 | $this->content = gzinflate($this->content); 172 | } 173 | // If $persist_cookies, deal with any cookies 174 | if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) { 175 | $cookies = $this->headers['set-cookie']; 176 | if (!is_array($cookies)) { 177 | $cookies = array($cookies); 178 | } 179 | foreach ($cookies as $cookie) { 180 | if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) { 181 | $this->cookies[$m[1]] = $m[2]; 182 | } 183 | } 184 | // Record domain of cookies for security reasons 185 | $this->cookie_host = $this->host; 186 | } 187 | // If $persist_referers, set the referer ready for the next request 188 | if ($this->persist_referers) { 189 | $this->debug('Persisting referer: ' . $this->getRequestURL()); 190 | $this->referer = $this->getRequestURL(); 191 | } 192 | // Finally, if handle_redirects and a redirect is sent, do that 193 | if ($this->handle_redirects) { 194 | if (++$this->redirect_count >= $this->max_redirects) { 195 | $this->errormsg = 'Number of redirects exceeded maximum (' . $this->max_redirects . ')'; 196 | $this->debug($this->errormsg); 197 | $this->redirect_count = 0; 198 | return false; 199 | } 200 | $location = isset($this->headers['location']) ? $this->headers['location'] : ''; 201 | $uri = isset($this->headers['uri']) ? $this->headers['uri'] : ''; 202 | if ($location || $uri) { 203 | $url = parse_url($location . $uri); 204 | // This will FAIL if redirect is to a different site 205 | return $this->get($url['path']); 206 | } 207 | } 208 | return true; 209 | } 210 | 211 | function buildRequest() { 212 | $headers = array(); 213 | $headers[] = "{$this->method} {$this->path} HTTP/1.0"; // Using 1.1 leads to all manner of problems, such as "chunked" encoding 214 | $headers[] = "Host: {$this->host}"; 215 | $headers[] = "User-Agent: {$this->user_agent}"; 216 | $headers[] = "Accept: {$this->accept}"; 217 | if ($this->use_gzip) { 218 | $headers[] = "Accept-encoding: {$this->accept_encoding}"; 219 | } 220 | $headers[] = "Accept-language: {$this->accept_language}"; 221 | if ($this->referer) { 222 | $headers[] = "Referer: {$this->referer}"; 223 | } 224 | // Cookies 225 | if ($this->cookies) { 226 | $cookie = 'Cookie: '; 227 | foreach ($this->cookies as $key => $value) { 228 | $cookie .= "$key=$value; "; 229 | } 230 | $headers[] = $cookie; 231 | } 232 | // Basic authentication 233 | if ($this->username && $this->password) { 234 | $headers[] = 'Authorization: BASIC ' . base64_encode($this->username . ':' . $this->password); 235 | } 236 | // If this is a POST, set the content type and length 237 | if ($this->postdata) { 238 | $headers[] = 'Content-Type: application/x-www-form-urlencoded'; 239 | $headers[] = 'Content-Length: ' . strlen($this->postdata); 240 | } 241 | $request = implode("\r\n", $headers) . "\r\n\r\n" . $this->postdata; 242 | return $request; 243 | } 244 | 245 | function getStatus() { 246 | return $this->status; 247 | } 248 | 249 | function getContent() { 250 | return $this->content; 251 | } 252 | 253 | function getHeaders() { 254 | return $this->headers; 255 | } 256 | 257 | function getHeader($header) { 258 | $header = strtolower($header); 259 | if (isset($this->headers[$header])) { 260 | return $this->headers[$header]; 261 | } else { 262 | return false; 263 | } 264 | } 265 | 266 | function getError() { 267 | return $this->errormsg; 268 | } 269 | 270 | function getCookies() { 271 | return $this->cookies; 272 | } 273 | 274 | function getRequestURL() { 275 | $url = 'http://' . $this->host; 276 | if ($this->port != 80) { 277 | $url .= ':' . $this->port; 278 | } 279 | $url .= $this->path; 280 | return $url; 281 | } 282 | 283 | // Setter methods 284 | function setUserAgent($string) { 285 | $this->user_agent = $string; 286 | } 287 | 288 | function setAuthorization($username, $password) { 289 | $this->username = $username; 290 | $this->password = $password; 291 | } 292 | 293 | function setCookies($array) { 294 | $this->cookies = $array; 295 | } 296 | 297 | // Option setting methods 298 | function useGzip($boolean) { 299 | $this->use_gzip = $boolean; 300 | } 301 | 302 | function setPersistCookies($boolean) { 303 | $this->persist_cookies = $boolean; 304 | } 305 | 306 | function setPersistReferers($boolean) { 307 | $this->persist_referers = $boolean; 308 | } 309 | 310 | function setHandleRedirects($boolean) { 311 | $this->handle_redirects = $boolean; 312 | } 313 | 314 | function setMaxRedirects($num) { 315 | $this->max_redirects = $num; 316 | } 317 | 318 | function setHeadersOnly($boolean) { 319 | $this->headers_only = $boolean; 320 | } 321 | 322 | function setDebug($boolean) { 323 | $this->debug = $boolean; 324 | } 325 | 326 | // "Quick" static methods 327 | function quickGet($url) { 328 | $bits = parse_url($url); 329 | $host = $bits['host']; 330 | $port = isset($bits['port']) ? $bits['port'] : 80; 331 | $path = isset($bits['path']) ? $bits['path'] : '/'; 332 | if (isset($bits['query'])) { 333 | $path .= '?' . $bits['query']; 334 | } 335 | $client = new HttpClient($host, $port); 336 | if (!$client->get($path)) { 337 | return false; 338 | } else { 339 | return $client->getContent(); 340 | } 341 | } 342 | 343 | static function quickPost($url, $data) { 344 | $bits = parse_url($url); 345 | $host = $bits['host']; 346 | $port = isset($bits['port']) ? $bits['port'] : 80; 347 | $path = isset($bits['path']) ? $bits['path'] : '/'; 348 | $client = new self($host, $port); 349 | if (!$client->post($path, $data)) { 350 | return false; 351 | } else { 352 | return $client->getContent(); 353 | } 354 | } 355 | 356 | function debug($msg, $object = false) { 357 | if ($this->debug) { 358 | print '
HttpClient Debug: ' . $msg; 359 | if ($object) { 360 | ob_start(); 361 | print_r($object); 362 | $content = htmlentities(ob_get_contents()); 363 | ob_end_clean(); 364 | print '
' . $content . '
'; 365 | } 366 | print '
'; 367 | } 368 | } 369 | 370 | } 371 | 372 | ?> --------------------------------------------------------------------------------