您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的Http服务-请求-获得请求参数get
发布时间:2021-12-04 23:53:21编辑:雪饮阅读()
获得请求参数get
获取整个get数组
$get = $request->get();
如果请求没有get参数则返回一个空的数组。
获取get数组的某一个值
$name = $request->get('name');
如果get数组中不包含这个值则返回null。
你也可以给get方法第二个参数传递一个默认值,如果get数组中没找到对应值则返回默认值。例如:
$name = $request->get('name', 'tom');
实例:
<?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("get请求参数:");
echo "\r\n";
var_dump($request->get());
echo "\r\n";
var_dump("get请求参数name:");
echo "\r\n";
var_dump($request->get("name"));
echo "\r\n";
var_dump("get请求参数kill:");
echo "\r\n";
var_dump($request->get("kill","暂无kill参数!"));
echo "\r\n";
$connection->send("hello");
};
// 运行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.
string(18) "get请求参数:"
array(1) {
["name"]=>
string(9) "snowDrink"
}
string(22) "get请求参数name:"
string(9) "snowDrink"
string(22) "get请求参数kill:"
string(19) "暂无kill参数!"
客户端连接传参:
[root@izj6c2jeancylo0ppo4vz5z ~]# elinks 127.0.0.1:8484/?name=snowDrink --dump
hello
关键字词:workerman,Http,get,请求
相关文章
- workerman的AsyncUdpConnection类的send方法
- workerman的AsyncUdpConnection类的__construct构造方
- workerman的AsyncTcpConnection类的transport属性都利
- workerman的AsyncTcpConnection类的reConnect 方法实
- workerman的AsyncTcpConnection类的connect 方法实现
- workerman类TcpConnection的接口send
- workerman类TcpConnection的接口pipe通道实现流量控制
- workerman类TcpConnection的接口pauseRecv实现上传流
- workerman类TcpConnection的接口pauseRecv实现上传流
- workerman类TcpConnection的worker属性