JWT认证实现
PHP 8.1+
<?php
// JWT生成示例
use Firebase\JWT\JWT;
$key = 'secret_key';
$payload = [
'iss' => 'your_domain',
'aud' => 'your_client',
'iat' => time(),
'exp' => time() + 3600,
'data' => [
'userId' => 123,
'email' => 'user@example.com'
]
];
$jwt = JWT::encode($payload, $key, 'HS256');
echo $jwt;
?>
异步任务处理
Swoole扩展
<?php
$server = new Swoole\HTTP\Server("0.0.0.0", 9501);
$server->on('request', function ($request, $response) {
// 异步任务
$taskId = $server->task([
'type' => 'log',
'data' => $request->get
]);
$response->header('Content-Type', 'text/plain');
$response->end("Task $taskId received\n");
});
// 任务处理回调
$server->on('task', function ($server, $taskId, $data) {
// 处理耗时任务
file_put_contents('task.log', json_encode($data));
return 'Task Finished';
});
$server->start();
?>