您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的Http服务-请求-获取header
发布时间:2021-12-04 23:59:44编辑:雪饮阅读()
获取header
获取整个header数组
$headers = $request->header();
如果请求没有header参数则返回一个空的数组。注意所有key均为小写。
获取header数组的某一个值
$host = $request->header('host');
如果header数组中不包含这个值则返回null。注意所有key均为小写。
与get方法一样,你也可以给header方法第二个参数传递一个默认值,如果header数组中没找到对应值则返回默认值。例如:
$host = $request->header('host', 'localhost');
实例:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:8484');
$worker->onMessage = function(TcpConnection $connection, Request $request)
{
// $request为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
var_dump("header参数:");
echo "\r\n";
var_dump($request->header());
echo "\r\n";
var_dump("header参数user-agent:");
echo "\r\n";
var_dump($request->header('user-agent'));
echo "\r\n";
var_dump("header参数token:");
echo "\r\n";
var_dump($request->header('token', '暂无token'));
echo "\r\n";
$connection->send("hello\r\n");
};
// 运行worker
Worker::runAll();
?>
实例运行并有curl客户端请求(带data)时:
[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.
string(15) "header参数:"
array(5) {
["user-agent"]=>
string(11) "curl/7.29.0"
["host"]=>
string(14) "127.0.0.1:8484"
["accept"]=>
string(3) "*/*"
["content-length"]=>
string(2) "11"
["content-type"]=>
string(33) "application/x-www-form-urlencoded"
}
string(25) "header参数user-agent:"
string(11) "curl/7.29.0"
string(20) "header参数token:"
string(11) "暂无token"
curl客户端请求(带data):
[root@izj6c2jeancylo0ppo4vz5z ~]# curl --location --request POST 'http://127.0.0.1:8484' --data '{user:"xy"}'
hello
发现这里curl请求不带data时候,服务端onMessage都不触发:
[root@izj6c2jeancylo0ppo4vz5z ~]# curl --location --request POST 'http://127.0.0.1:8484'
关键字词:workerman,Http,header
相关文章
- workerman的Http服务-请求-获取cookie
- workerman的Http服务-请求-获得原始请求post包体
- workerman的Http服务-请求-获得请求对象
- workerman的Http服务-请求-获得请求参数post
- workerman的Http服务-请求-获得请求参数get
- workerman的AsyncUdpConnection类的send方法
- workerman的AsyncUdpConnection类的__construct构造方
- workerman的AsyncTcpConnection类的transport属性都利
- workerman的AsyncTcpConnection类的reConnect 方法实
- workerman的AsyncTcpConnection类的connect 方法实现