您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman的Http服务-请求-获得请求参数post
发布时间:2021-12-04 23:54:45编辑:雪饮阅读()
获取整个post数组
$post = $request->post();
如果请求没有post参数则返回一个空的数组。
获取post数组的某一个值
$name = $request->post('name');
如果post数组中不包含这个值则返回null。
与get方法一样,你也可以给post方法第二个参数传递一个默认值,如果post数组中没找到对应值则返回默认值。例如:
$name = $request->post('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("post请求参数:");
echo "\r\n";
var_dump($request->post());
echo "\r\n";
var_dump("post请求参数name:");
echo "\r\n";
var_dump($request->post("name"));
echo "\r\n";
var_dump("post请求参数kill:");
echo "\r\n";
var_dump($request->post("kill","暂无kill参数!"));
echo "\r\n";
$connection->send("hello\r\n");
};
// 运行worker
Worker::runAll();
?>
实例运行并有post客户端传参请求:
[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(19) "post请求参数:"
array(1) {
["name"]=>
string(11) ""snowDrink""
}
string(23) "post请求参数name:"
string(11) ""snowDrink""
string(23) "post请求参数kill:"
string(19) "暂无kill参数!"
客户端post带参请求:
[root@izj6c2jeancylo0ppo4vz5z ~]# curl --location --request POST 'http://127.0.0.1:8484' --form 'name="snowDrink"'
hello
关键字词:workerman,Http,post,请求
相关文章
- 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实现上传流