<?php $http=new swoole_http_server('0.0.0.0',9501); $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.phpphp
<?php $http=new swoole_http_server('0.0.0.0',9501); $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',9501); $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',9501); $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',9501); $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(404); $response->end('404 not found'); } }); $http->start();
運行在nginx服務器react
運行在swoole--http_server服務器nginx
<?php $http=new swoole_http_server('0.0.0.0',9501); $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(404); $response->end('404 not found'); } }); $http->start();
設置完成後(daemonize=1時),http_server服務器轉入後臺做爲守護進程運行git
<?php $http=new swoole_http_server('0.0.0.0',9501); $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{ //靜態請求處理 $mimes=include('mimes.php'); $response->header("Content-type",$mimes[$ext]); $content=file_get_contents($filename); $response->end($content); } }else{ $response->status(404); $response->end('404 not found'); } }); $http->start();
ZPHP框架github
https://github.com/shenzhe/zphpweb
執行腳手架生成項目php框架
將生成的項目文件所有複製到chat目錄下 cp -rf chat_zphp/* chat//服務器
將zphp框架文件所有複製到chat目錄下 cp -rf zphp/* chat//swoole
<?php use ZPHP\ZPHP; $zphp=null; $mimes=null; $http=new swoole_http_server('0.0.0.0',9503); $http->set([ 'worker_num'=>16, 'daemonize'=>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); ************************/ $response->status(404); $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進程 業務邏輯處理 ************************************************************************/
代碼熱更新cookie
<?php use ZPHP\ZPHP; $zphp=null; $mimes=null; $http=new swoole_http_server('0.0.0.0',9503); $http->set([ 'worker_num'=>16, 'daemonize'=>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); ************************/ $response->status(404); $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; }