php return 返回html_【php socket通讯】php实现http服务
生活随笔
收集整理的這篇文章主要介紹了
php return 返回html_【php socket通讯】php实现http服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http服務是建立在tcp服務之上的,它是tcp/ip協議的應用,前面我們已經實現了tcp服務,并且使用三種不同的方式連接tcp服務
php中連接tcp服務的三種方式
既然http也是tcp應用層的一種,那么我們直接使用瀏覽器來連接tcp服務可不可以?答案是可以的,只不過連接之后直接返回給瀏覽器的信息,瀏覽器不能夠正確的識別出來。那么怎么才能讓瀏覽器正確的識別tcp服務返回的信息呢?
這個時候我們就需要使用到http協議啦,至于http傳輸中都傳了哪些信息可以在瀏覽器中 f12 查看
目錄結構:
http_serv.php文件
<?php /** * Http 服務器類 */ class Http{private $host;private $port;private $_root;public $mime_types = array('avi' => 'video/x-msvideo','bmp' => 'image/bmp','css' => 'text/css','doc' => 'application/msword','gif' => 'image/gif','htm' => 'text/html','html' => 'text/html','ico' => 'image/x-icon','jpe' => 'image/jpeg','jpeg' => 'image/jpeg','jpg' => 'image/jpeg','js' => 'application/x-javascript','mpeg' => 'video/mpeg','ogg' => 'application/ogg','png' => 'image/png','rtf' => 'text/rtf','rtx' => 'text/richtext','swf' => 'application/x-shockwave-flash','wav' => 'audio/x-wav','wbmp' => 'image/vnd.wap.wbmp','zip' => 'application/zip',);/*** @param string $host 監聽地址* @param int $port 監聽端口* @param string $_root 網站根目錄*/public function __construct($host,$port,$_root){$this->host = $host;$this->port = $port;$this->_root = $_root;}/*** 啟動http服務*/public function start(){//創建socket套接字$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//設置阻塞模式socket_set_block($socket);//為套接字綁定ip和端口socket_bind($socket,$this->host,$this->port);//監聽socketsocket_listen($socket,4);while(true){//接收客戶端請求if(($msgsocket = socket_accept($socket)) !== false){//讀取請求內容$buf = socket_read($msgsocket, 9024);preg_match("//(.*) HTTP/1.1/",$buf,$matchs);preg_match("/Accept: (.*?),/",$buf,$matchss);//獲取接收文件類型$type = explode("/",$matchss[1])[0];if($type=="text"){$content = $this->GetString($matchs[1]);}else{$content = $this->GetImg($matchs[1]);}socket_write($msgsocket,$content,strlen($content));socket_close($msgsocket);}}}/*** 組裝消息頭信息模板* @param int $code 狀態碼* @param string $status 狀態名稱* @param string $content 發送的文本內容* @param string $content_type 發送的內容類型* @return string**/public function GetHeaders($code,$status,$content="",$content_type="text/html;charset=utf-8"){$header = '';$header .= "HTTP/1.1 {$code} {$status}rn";$header .= "Date: ".gmdate('D, d M Y H:i:s T')."rn";$header .= "Content-Type: {$content_type}rn";$header .= "Content-Length: ".strlen($content)."rnrn";//必須2個rn表示頭部信息結束$header .= $content;return $header;}/*** 組裝文本發送信息* @param string $url_path* @return string**/public function GetString($url_path){if($this->getRealPath($url_path)){if(is_readable($this->getRealPath($url_path))){return $this->GetHeaders(200,"OK",file_get_contents($this->getRealPath($url_path)),$this->getMime($url_path));}else{return $this->GetHeaders(401,"Unauthorized");}}else{return $this->GetHeaders(404,"Not Found");}}/*** 組裝資源返回信息* @param string $url_path* @return string**/public function GetImg($url_path){if($this->getRealPath($url_path)){return $this->GetHeaders(200,"OK",file_get_contents($this->getRealPath($url_path)),$this->getMime($url_path));}else{return $this->GetHeaders(404,"Not Found");}}/*** 獲取資源類型* @param string $path* @return mixed*/public function getMime($path){$type = explode(".",$path);$mime = $this-> mime_types[$type[1]]; return $mime;}/*** 獲取訪問資源的真實地址* @param $url_path* @return bool|string*/public function getRealPath($url_path){return realpath($this->_root."/".$url_path);}}$server = new Http("127.0.0.1",3046,"wwwroot"); $server->start();效果圖:
以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要
PHP進階架構師>>>視頻、面試文檔免費獲取?shimo.im或 者關注咱們下面的知乎專欄
PHP大神進階?zhuanlan.zhihu.com 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的php return 返回html_【php socket通讯】php实现http服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos losf 安装_Linux
- 下一篇: as安装过程中gradle_重新认识An