HttpRequest.class.php
<?php /** HttpRequest class, HTTP請求類,支持GET,POST,Multipart/form-data * Date: 2013-09-25 * Author: fdipzone * Ver: 1.0 * * Func: * public setConfig 設置連接參數 * public setFormdata 設置表單數據 * public setFiledata 設置文件數據 * public send 發送數據 * private connect 創建連接 * private disconnect 斷開連接 * private sendGet get 方式,處理發送的數據,不會處理文件數據 * private sendPost post 方式,處理發送的數據 * private sendMultipart multipart 方式,處理發送的數據,發送文件推薦使用此方式 */ class HttpRequest{ // class start private $_ip = ''; private $_host = ''; private $_url = ''; private $_port = ''; private $_errno = ''; private $_errstr = ''; private $_timeout = 15; private $_fp = null; private $_formdata = array(); private $_filedata = array(); // 設置連接參數 public function setConfig($config){ $this->_ip = isset($config['ip'])? $config['ip'] : ''; $this->_host = isset($config['host'])? $config['host'] : ''; $this->_url = isset($config['url'])? $config['url'] : ''; $this->_port = isset($config['port'])? $config['port'] : ''; $this->_errno = isset($config['errno'])? $config['errno'] : ''; $this->_errstr = isset($config['errstr'])? $config['errstr'] : ''; $this->_timeout = isset($confg['timeout'])? $confg['timeout'] : 15; // 如沒有設置ip,則用host代替 if($this->_ip==''){ $this->_ip = $this->_host; } } // 設置表單數據 public function setFormData($formdata=array()){ $this->_formdata = $formdata; } // 設置文件數據 public function setFileData($filedata=array()){ $this->_filedata = $filedata; } // 發送數據 public function send($type='get'){ $type = strtolower($type); // 檢查發送類型 if(!in_array($type, array('get','post','multipart'))){ return false; } // 檢查連接 if($this->connect()){ switch($type){ case 'get': $out = $this->sendGet(); break; case 'post': $out = $this->sendPost(); break; case 'multipart': $out = $this->sendMultipart(); break; } // 空數據 if(!$out){ return false; } // 發送數據 fputs($this->_fp, $out); // 讀取返回數據 $response = ''; while($row = fread($this->_fp, 4096)){ $response .= $row; } // 斷開連接 $this->disconnect(); $pos = strpos($response, "rnrn"); $response = substr($response, $pos+4); return $response; }else{ return false; } } // 創建連接 // 查看本欄目