?? socket.php
字號:
<?php/*Socket數據交互類*/class Easy_Socket{ private $_fp; public $timeout; public function __construct(){ } /* 鏈接目標服務器 @param string $target 目標地址 @param int $port 端口號 @param int $timeout 超時時間 */ public function Connect($target, $port=80, $timeout=10){ $this->timeout = intval($timeout); $this->target = $target; $this->port = (int)$port; //echo $target; $this->_fp = @fsockopen($this->target, $this->port, $errno, $error, $this->timeout); if (!$this->_fp){ $this->errno = $errno; $this->error = $error; return false; }else{ stream_set_blocking($this->_fp, 1); unset($target, $port, $timeout); return true; } } /* 從服務器端獲取數據 @param array $Query 提交的GET數據 return bool; */ public function setQuery($Query = array()){ if (is_resource($this->_fp)){ if (isset($Query['host']) === false ){ $Query['host'] = 'google.com'; } if (isset($Query['referer']) === false ){ $Query['referer'] = 'www.baidu.com'; } if (isset($Query['path'])){ $Query['path'] = '/'.$Query['path']; }else{ $Query['path'] = '/'; } //echo $Query['path']; $send = "GET {$Query['path']} HTTP/1.1\r\n"; $send.= "Host:{$Query['host']}\r\n"; //$send.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5\r\n"; $send.= "Referer:http://{$Query['referer']}/\r\n"; //$send.= "Content-Length: ".strlen($Query['file'])."\r\n"; $send.= "Connection: Close\r\n\r\n"; fwrite($this->_fp, $send); while ( !feof( $this->_fp) ) { $state = fgets($this->_fp, 128); if (stristr($state, 'HTTP/1.1 200 OK') === true){ return true; }else{ $this->errno = $state; $this->error = '服務器內部'.$state.'請求錯誤!'; return true; } } $this->error = '服務器內部'.fgets($this->_fp, 128).'請求錯誤!'; return false; }else{ $this->error = ('必須現鏈接服務器'); return false; } } /* 從客戶端提交數據到服務器 */ public function getContent(){ $header = fgets($this->_fp, 4096); while ( !feof( $this->_fp ) ) { $data.= fgets( $this->_fp , 4096); } $array = explode("\r\n\r\n", $data); $data = ''; if (count($array)>2){ $data['headers'] = array_shift($array); $data['body'] = implode('', $array); }else{ $data['headers'] = $array[0]; $data['body'] = $array[1]; } return $data; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -