您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的http服务-响应-更改状态码 - Response类初始化实例化之后
发布时间:2021-12-06 23:27:59编辑:雪饮阅读()
当Response类已经初始化后,想更改状态码使用下面方法。
实例:
<?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)
{
$response = new Response(200);
if ($request->path() === '/404') {
$response->withStatus(404);
$connection->send($response);
} 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请求:
[root@izj6c2jeancylo0ppo4vz5z ~]# wget http://127.0.0.1:8484/404/ && cat index.html
--2021-12-06 16:08:18-- http://127.0.0.1:8484/404/
Connecting to 127.0.0.1:8484... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12 [text/html]
Saving to: ‘index.html.5’
100%[======================================================================================================================================================================>] 12 --.-K/s in 0s
2021-12-06 16:08:18 (3.06 MB/s) - ‘index.html.5’ saved [12/12]
this is body
404请求:
[root@izj6c2jeancylo0ppo4vz5z ~]# wget http://127.0.0.1:8484/404
--2021-12-06 16:08:24-- http://127.0.0.1:8484/404
Connecting to 127.0.0.1:8484... connected.
HTTP request sent, awaiting response... 404 Not Found
2021-12-06 16:08:24 ERROR 404: Not Found.
关键字词: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
- workerman的http服务-请求-获取指定上传文件