<?php
$http = new swoole_http_server('0.0.0.0', 8888);
//设置回调函数,当收到请求时,会回调此函数
$http->on('request', function($request, $response) {
$pathInfo = $request->server['path_info'];
$file = __DIR__ . $pathInfo;
if(is_file($file)) {
//判断文件后缀名
if(pathinfo($pathInfo)['extension'] == 'php') {
ob_start();
include $file;
$content = ob_get_contents();
ob_end_clean();
$response->end($content);
} else {
//处理其他文件
}
} else {
$response->status(404);
$response->end('not found');
}
});
//启动http服务
$http->start();
centos7环境下swoole1.9的安装与HttpServer的使用方法分(2)
我们在http.php同目录下创建1.php脚本,然后请求192.168.1.222:8888/1.php就可正常访问。
3、HttpServer的守护进程化?
只需设置配置参数daemonize为1就可以了。
<?php
$http = new swoole_http_server('0.0.0.0', 8888);
//设置进程数量,和守护进程化
$http->set(array(
'worker_num' => 4,
'daemonize' => 1,
));
//设置回调函数,当收到请求时,会回调此函数
$http->on('request', function($request, $response) {
$pathInfo = $request->server['path_info'];
$file = __DIR__ . $pathInfo;
if(is_file($file)) {
//判断文件后缀名
if(pathinfo($pathInfo)['extension'] == 'php') {
ob_start();
include $file;
$content = ob_get_contents();
ob_end_clean();
$response->end($content);
} else {
}
} else {
$response->status(404);
$response->end('not found');
}
});
//启动http服务
$http->start();
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《php socket用法总结》、《php面向对象程序设计入门教程》、《PHP数据结构与算法教程》及《php程序设计算法总结》
希望本文所述对大家PHP程序设计有所帮助。
