<?php
$http=new swoole_http_server('0.0.0.0',); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
print_r($request);
}); $http->start();
http://192.168.10.31:9501/swoole/chat/chat/http_server.php
<?php
$http=new swoole_http_server('0.0.0.0',); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
$response->end("hello world");
}); $http->start();
<?php
$http=new swoole_http_server('0.0.0.0',); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
$response->status(404);
$response->end('404 not found');
}); $http->start();
<?php
$http=new swoole_http_server('0.0.0.0',); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
//$response->status(404);
//$response->end('404 not found');
$pathinfo=$request->server['path_info'];
$in=strrpos($pathinfo,'/');
$filename=substr($pathinfo,$in);
$filename=__DIR__.$pathinfo;
if(is_file($filename)){
$content=file_get_contents($filename);
$response->end($content);
}else{
$response->status(404);
$response->end('404 not found');
}
}); $http->start();
<?php
$http=new swoole_http_server('0.0.0.0',); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
//$response->status(404);
//$response->end('404 not found');
$pathinfo=$request->server['path_info'];
$in=strrpos($pathinfo,'/');
$filename=substr($pathinfo,$in);
$filename=__DIR__.$filename;
if(is_file($filename)){
$ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
//动态请求处理
if($ext=='php'){
ob_start();
include_once $filename;
$content=ob_get_contents();
ob_end_clean();
$response->end($content);
}else{ //静态请求处理
$content=file_get_contents($filename);
$response->end($content);
}
}else{
$response->status();
$response->end('404 not found');
}
}); $http->start();
运行在nginx服务器
运行在swoole--http_server服务器
<?php
$http=new swoole_http_server('0.0.0.0',); $http->set([
'worker_num'=>16,
'daemonize'=>1
]); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
//$response->status(404);
//$response->end('404 not found');
$pathinfo=$request->server['path_info'];
$in=strrpos($pathinfo,'/');
$filename=substr($pathinfo,$in);
$filename=__DIR__.$filename;
if(is_file($filename)){
$ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
//动态请求处理
if($ext=='php'){
ob_start();
include_once $filename;
$content=ob_get_contents();
ob_end_clean();
$response->end($content);
}else{ //静态请求处理
$content=file_get_contents($filename);
$response->end($content);
}
}else{
$response->status();
$response->end('404 not found');
}
}); $http->start();
设置完成后(daemonize=1时),http_server服务器转入后台作为守护进程运行
<?php
$http=new swoole_http_server('0.0.0.0',); $http->set([
'worker_num'=>,
'daemonize'=>
]); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
//$response->status(404);
//$response->end('404 not found');
$pathinfo=$request->server['path_info'];
$in=strrpos($pathinfo,'/');
$filename=substr($pathinfo,$in);
$filename=__DIR__.$filename;
if(is_file($filename)){
$ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
//动态请求处理
if($ext=='php'){
ob_start();
include_once $filename;
$content=ob_get_contents();
ob_end_clean();
$response->end($content);
}else{ //静态请求处理
$mimes=include('mimes.php');
$response->header("Content-type",$mimes[$ext]);
$content=file_get_contents($filename);
$response->end($content);
}
}else{
$response->status();
$response->end('404 not found');
}
}); $http->start();
ZPHP框架
https://github.com/shenzhe/zphp
执行脚手架生成项目
将生成的项目文件全部复制到chat目录下 cp -rf chat_zphp/* chat//
将zphp框架文件全部复制到chat目录下 cp -rf zphp/* chat//
<?php
use ZPHP\ZPHP; $zphp=null;
$mimes=null; $http=new swoole_http_server('0.0.0.0',); $http->set([
'worker_num'=>,
'daemonize'=>
]); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
//$response->status(404);
//$response->end('404 not found');
$pathinfo=$request->server['path_info'];
$in=strrpos($pathinfo,'/');
$filename=substr($pathinfo,$in);
$filename=__DIR__.$filename;
if(is_file($filename)){
$ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
//动态请求处理
if($ext=='php'){
/************************
ob_start();
include_once $filename;
$content=ob_get_contents();
ob_end_clean();
$response->end($content);
************************/
$response->status();
$response->end('404 not found');
}else{ //静态请求处理
//$mimes=include('mimes.php');
global $mimes;
$response->header("Content-type",$mimes[$ext]);
$content=file_get_contents($filename);
$response->end($content);
}
}else{
//$response->status(404);
//$response->end('404 not found');
global $zphp;
ob_start();
$zphp->run();
$result=ob_get_contents();
ob_end_clean();
$response->end($result);
}
}); $http->on('workerStart',function($serv,$worker_id){
//require zphp框架目录地址
require __DIR__.DIRECTORY_SEPARATOR.'zphp'.DIRECTORY_SEPARATOR.'ZPHP'.DIRECTORY_SEPARATOR.'ZPHP.php';
//home/wwwroot/default/swoole/www.zphp.com 是应用的地址
$webPath=__DIR__; //项目目录
$configPath='default';//配置的目录的名称
global $zphp;
$zphp=ZPHP::run($webPath,false,$configPath);
global $mimes;
$mimes=require 'mimes.php';
});
$http->start(); /************************************************************************
master //连接的处理(收发管理) 多线程(reactor,heartbeat) epoll
manager //fork 管理worker进程
worker //worker进程 业务逻辑处理
************************************************************************/
代码热更新
<?php
use ZPHP\ZPHP; $zphp=null;
$mimes=null; $http=new swoole_http_server('0.0.0.0',); $http->set([
'worker_num'=>,
'daemonize'=>
]); $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
//print_r($request);
//$response->end("hello world");
//$response->status(404);
//$response->end('404 not found');
$pathinfo=$request->server['path_info'];
$in=strrpos($pathinfo,'/');
$filename=substr($pathinfo,$in);
$filename=__DIR__.$filename;
if(is_file($filename)){
$ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
//动态请求处理
if($ext=='php'){
/************************
ob_start();
include_once $filename;
$content=ob_get_contents();
ob_end_clean();
$response->end($content);
************************/
$response->status();
$response->end('404 not found');
}else{ //静态请求处理
//$mimes=include('mimes.php');
global $mimes;
$response->header("Content-type",$mimes[$ext]);
$content=file_get_contents($filename);
$response->end($content);
}
}else{
//$response->status(404);
//$response->end('404 not found');
$_GET=$_POST=$_COOKIE=$_REQUEST=[];
if(!empty($request->get)){
$_GET=$request->get;
$_REQUEST +=$_GET;
}
if(!empty($request->post)){
$_POST=$request->post;
$_REQUEST +=$_POST;
}
if(!empty($request->cookie)){
$_COOKIE=$request->cookie;
}
global $zphp;
ob_start();
$zphp->run();
$result=ob_get_contents();
ob_end_clean();
$response->end($result);
}
}); $http->on('workerStart',function($serv,$worker_id){
//require zphp框架目录地址
require __DIR__.DIRECTORY_SEPARATOR.'zphp'.DIRECTORY_SEPARATOR.'ZPHP'.DIRECTORY_SEPARATOR.'ZPHP.php';
//home/wwwroot/default/swoole/www.zphp.com 是应用的地址
$webPath=__DIR__; //项目目录
$configPath='default';//配置的目录的名称
global $zphp;
$zphp=ZPHP::run($webPath,false,$configPath);
global $mimes;
$mimes=require 'mimes.php';
});
$http->start(); /************************************************************************
master //连接的处理(收发管理) 多线程(reactor,heartbeat) epoll
manager //fork 管理worker进程
worker //worker进程 业务逻辑处理
************************************************************************/
D:\phpStudy\WWW\swoole\chat\apps\ctrl\main\main.php
public function main()
{
$project = Config::getField('project', 'name', 'zphp');
$data = $project." runing in swoole!\n";
$params = $_REQUEST;//Request::getParams();
if(!empty($params)) {
foreach($params as $key=>$val) {
$data.= "key:{$key}=>{$val}\n";
}
}
print_r($data);
return $data;
}