使用纯php构建一个简单的PHP服务器
使用原生PHP构建一个简单的PHPWeb服务器
1.目录机构
webserver
--src
-- Response.php
-- Server.php
-- Request.php
-- vendor
-- Server.php
-- composer.json
2. 使用comoposer构建自动加载
编辑`comooser.json`文件
{
"autoload": {
"psr-4": {
"Icarus\\Station\\PHPServer\\": "src/"
}
}
}
使用PSR-4自动加载方式构建自动加载
3. 编写 Server文件
该文件作为启动文件,使用以下命令 php Server 8080 启动服务
<?php
use Icarus\Station\PHPServer\Server;
use Icarus\Station\PHPServer\Request;
use Icarus\Station\PHPServer\Response;
array_shift($argv);
//获取端口号
if (empty($argv)) {
$port = 80;
}else {
$port = (int)array_shift($argv);
}
require_once "./vendor/autoload.php";
$server = new Server("127.0.0.1",$port);
$server->listen(function(Request $request){
return new Response("Hello World!");
});
4. 编写Response.php
该类实现对请求的响应
<?php
namespace Icarus\Station\PHPServer;
class Response
{
protected static $statusCodes = [
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
];
protected $status = 200;
protected $body = '';
protected $headers = [];
public function __construct($body, $status = null)
{
if (!is_null($status)) {
$this->status = $status;
}
$this->body = $body;
$this->header('Date', gmdate('D, d M Y H:i:s T'));
$this->header('Content-Type', 'text/html; charset=utf-8');
$this->header('Server', 'PHPServer/1.0.0');
}
public function header($key, $val)
{
$this->headers[ucwords($key)] = $val;
}
public function buildHeaderString()
{
$lines = [];
$lines[] = "HTTP/1.1 " . $this->status . " " . static::$statusCodes[$this->status];
foreach ($this->headers as $key => $value) {
$lines[] = $key . ": " . $value;
}
return implode(" \r\n", $lines) . "\r\n\r\n";
}
public static function error($statusCode)
{
header(self::$statusCodes[$statusCode], '', $statusCode);
}
public function __toString()
{
return $this->buildHeaderString() . $this->body;
}
}
5. 编写Request.php
该类主要实现请求的解析(暂时为GET请求)
<?php
namespace Icarus\Station\PHPServer;
class Request
{
protected $uri = '';
protected $method = '';
protected $params = [];
protected $headers = [];
public function __construct($method, $uri, $headers)
{
$this->method = strtolower($method);
$this->headers = $headers;
list($this->uri, $param) = explode('?', $uri);
parse_str($param, $this->params);
}
public function method()
{
return $this->method;
}
public function headers($key, $default = null)
{
if (isset($this->headers[$key])) {
$default = $this->headers[$key];
}
return $default;
}
public function uri()
{
return $this->uri;
}
public function params($key, $default = null)
{
if (isset($this->params[$key])) {
$default = $this->params($key);
}
return $default;
}
public static function withHeaderString($data)
{
$headers = explode("\n", $data);
list($method, $uri) = explode(" ", array_shift($headers));
$header = [];
foreach ($headers as $value) {
$value = trim($value);
if (strpos($value, ":") !== false) {
list($key, $value) = explode(":", $value);
$header[$key] = $value;
}
}
return new static($method, $uri, $header);
}
public function __toString()
{
return json_encode($this->headers);
}
}
6.编写Server.php
该模块主要实现对socket的封装。
<?php
namespace Icarus\Station\PHPServer;
class Server
{
protected $host = null;
protected $port = null;
protected $socket = null;
public function __construct($host, $port)
{
$this->host = $host;
$this->port = $port;
$this->createSocket();
$this->bind();
}
protected function createSocket()
{
$this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
}
protected function bind()
{
if (!socket_bind($this->socket,$this->host, $this->port)) {
throw new \Exception("未能绑定socket: " . $this->host . $this->port . socket_strerror(socket_last_error()));
}
}
public function listen(callable $callback)
{
while (true) {
socket_listen($this->socket);
if (!$client = socket_accept($this->socket)) {
socket_close($client);
continue;
}
$data = socket_read($client, 1024);
$request = Request::withHeaderString($data);
$response = call_user_func($callback, $request);
if (!$response | !$response instanceof Response) {
$response = Response::error(404);
}
$response = (string)$response;
socket_write($client,$response,strlen($response));
socket_close($client);
}
}
}
7. 使用
1. 进入到项目目录
2. 执行` php Server 8080`
3. 另起一个终端,执行 `curl "http://127.0.0.1:8080`
4. 显示 `Hello World!`
8. 总结
该demo使用socket来实现一个简单的webserver,但是由于php不支持多线程的的特性(其实也能实现,不过需要安装pthreads扩展),还是不适合开发webserver,此demo主要用来学习webserver的概念。
此demo参考了http://station.clancats.com/writing-a-webserver-in-pure-php/的讲解及实现。
使用纯php构建一个简单的PHP服务器的更多相关文章
- 通过python 构建一个简单的聊天服务器
构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使 ...
- struts1:(Struts重构)构建一个简单的基于MVC模式的JavaWeb
在构建一个简单的基于MVC模式的JavaWeb 中,我们使用了JSP+Servlet+JavaBean构建了一个基于MVC模式的简单登录系统,但在其小结中已经指出,这种模式下的Controller 和 ...
- 【Android Developers Training】 3. 构建一个简单UI
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 构建一个简单的Linux系统 MenuOs —— start_kernel到init进程(20135304刘世鹏)
构建一个简单的Linux系统 MenuOs —— start_kernel到init进程 作者:刘世鹏20135304 <Linux内核分析>MOOC课程http://mooc.study ...
- gRPC初探——概念介绍以及如何构建一个简单的gRPC服务
目录 引言 1. gRPC简介 2. 使用Protocol Buffers进行服务定义 2.1 定义消息 2.2 定义服务接口 3.构建简单的gRPC服务 3.1 编写proto文件,定义消息和接口 ...
- 第三周——构建一个简单的Linux系统MenuOS
[洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周 构建一个 ...
- 构建一个简单的基于MVC模式的JavaWeb
零晨三点半了,刚刚几个兄弟一起出去吼歌,才回来,这应该是我大学第二次去K歌,第一次是大一吧,之后每次兄弟喊我,我都不想去,因为我还是很害怕去KTV,或许是因为那里是我伤心的地方,也或许是因为我在那里失 ...
- 手把手教你用vue-cli构建一个简单的路由应用
上一章说道:十分钟上手-搭建vue开发环境(新手教程)https://www.jianshu.com/p/0c6678671635 开发环境搭建好之后,那么开始新添加一些页面,构建最基本的vue项目, ...
- Ant—使用Ant构建一个简单的Java工程(两)
博客<Ant-使用Ant构建一个简单的Java项目(一)>演示了使用Ant工具构建简单的Java项目,接着这个样例来进一步学习Ant: 上面样例须要运行多条ant命令才干运行Test类中的 ...
随机推荐
- js关于小数点失精算法修正0.07*100竟然=7.000000000000001
转发 https://blog.csdn.net/iteye_13003/article/details/82645716
- adb server version (31) doesn’t match this client (36); killing…
版权声明:蜜蜂采花酿蜂蜜,奶牛吃草产牛奶. https://blog.csdn.net/codehxy/article/details/52175186 案例1 报错信息如下 C:\Users\lin ...
- redis下载及安装教程
https://blog.csdn.net/w546097639/article/details/88547486
- 阿里HBase的数据管道设施实践与演进
摘要:第九届中国数据库技术大会,阿里巴巴技术专家孟庆义对阿里HBase的数据管道设施实践与演进进行了讲解.主要从数据导入场景. HBase Bulkload功能.HImporter系统.数据导出场景. ...
- python全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
昨日内容回顾 线程 什么是线程? 线程是cpu调度的最小单位 进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的 ...
- socket函数库简单封装
#pragma once #ifndef WINSOCK_H #include<WinSock2.h> #pragma comment(lib,"ws2_32.lib" ...
- 笨办法学Python(learn python the hard way)--练习程序31-35
下面是练习31-练习35,基于python3 #ex31.py 1 print("You enter a dark room witn two doors. Do you go throug ...
- php面向对象编程(oop)基础知识示例解释
面向对象并不是一种技术,而是一种思想,是一种解决问题的最基本的思维方式!如何理解使用?OOP:面向对象编程 (直接代码说明) 1.面向对象的基本概念 示例demo: <?php header(& ...
- 重写LayoutParams,读取子View自定义属性
在EasyConstraintLayout内部定义一个静态类LayoutParams继承ConstraintLayout.LayoutParams,然后在构造方法中读取上面自定义的属性.我们通过裁剪的 ...
- Scala从入门到放弃(三)Scala的数组、映射、元组和集合
1.数组 1.1定长数组和变长数组 object ArrayDemo { def main(args: Array[String]): Unit = { //初始化一个长度为8的定长数组,其数组元素均 ...