本文小编为大家详细介绍“PHP怎么实现Http服务”,内容详细,步骤清晰,细节处理妥当,希望这篇“php怎么实现http服务”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
本文小编为大家详细介绍“PHP怎么实现Http服务”,内容详细,步骤清晰,细节处理妥当,希望这篇“php怎么实现http服务”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
目录结构:
http_serv.php文件
<?php
classhttp{
private$host;
private$port;
private$_root;
public$mime_types=array(
'avi'=>'video/x-msvideo',
'bmp'=>'image/bmp',
'CSS'=>'text/css',
'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',
);
publicfunction__construct($host,$port,$_root){
$this->host=$host;
$this->port=$port;
$this->_root=$_root;
}
publicfunctionstart(){
//创建Socket套接字
$socket=socket_create(AF_INET,SOCK_STREAM,SOL_tcp);
//设置阻塞模式
socket_set_block($socket);
//为套接字绑定ip和端口
socket_bind($socket,$this->host,$this->port);
//监听socket
socket_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);
}
}
}
publicfunctionGetHeaders($code,$status,$content="",$content_type="text/html;charset=utf-8"){
$header='';
$header.="HTTP/1.1{$code}{$status}\r\n";
$header.="Date:".gmdate('D,dMYH:i:sT')."\r\n";
$header.="Content-Type:{$content_type}\r\n";
$header.="Content-Length:".strlen($content)."\r\n\r\n";//必须2个\r\n表示头部信息结束
$header.=$content;
return$header;
}
publicfunctionGetString($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,"NotFound");
}
}
publicfunctionGetImg($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,"NotFound");
}
}
publicfunctiongetMime($path){
$type=explode(".",$path);
$mime=$this->mime_types[$type[1]];
return$mime;
}
publicfunctiongetRealPath($url_path){
returnrealpath($this->_root."/".$url_path);
}
}
$server=newHttp("127.0.0.1",3046,"wwwroot");
$server->start();
读到这里,这篇“php怎么实现http服务”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网PHP编程频道。
--结束END--
本文标题: php怎么实现http服务
本文链接: https://lsjlt.com/news/93659.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0