您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的http服务-响应-更改状态码
发布时间:2021-12-06 23:28:59编辑:雪饮阅读()
当需要自定义状态码、header、cookie时,需要使用Workerman\Protocols\Http\Response响应类。
例如下面例子在访问路径为/404时返回404的状态码,包体内容为<h1>抱歉,文件不存在</h1>。
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
if ($request->path() === '/404') {
$connection->send(new Response(404, [], '<h1>抱歉,文件不存在</h1>'));
} else {
$connection->send('this is body');
}
};
// 运行worker
Worker::runAll();
?>
实例运行:
[root@izj6c2jeancylo0ppo4vz5z workerman]# php test.php start
Workerman[test.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.0.33
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:8484 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
一个正常404请求与一个非404请求:
[root@izj6c2jeancylo0ppo4vz5z ~]# elinks http://127.0.0.1:8484/404 --dump
抱歉,文件不存在
[root@izj6c2jeancylo0ppo4vz5z ~]# elinks http://127.0.0.1:8484/404/ --dump
this is body
关键字词:workerman,http,响应,更改,状态码
相关文章
- workerman的http服务-响应-更改状态码 - Response类初
- workerman的http服务-响应-发送header
- workerman的http服务-响应-发送header - Response类初
- workerman的http服务-请求-获取请求sessionId
- workerman的http服务-请求-获取请求HTTP版本
- workerman的http服务-请求-获取请求queryString
- workerman的http服务-请求-获取请求路径
- workerman的http服务-请求-获取请求uri
- workerman的http服务-请求-获取请求方法
- workerman的http服务-请求-获取host