<?php
// 编写自己的http服务器
/**
 * 第1个参数, 0.0.0.0 表示监听本机所有的网址地址
 * 第2参数,6060 表示监听端口号  当前建议1-1024之间是系统保留不要去用
 */
$http = new swoole_http_server('0.0.0.0', 6060);
// 设置
$http->set([
    // 工作的进程数量
    'worker_num'  => 2,
    // 一个进程请求的最大次数
    'max_request' => 1000
]);
// 相关事件的监听
// 开始事件
$http->on('start', function (swoole_server $server) {
    echo "服务启动了!\n";
});
// 处理请求事件
/**
 * $request 请求对象  服务器请求得到客户发过来的数据对象
 * $response 响应对象 服务器向客户发送数据所用的对象
 */
$http->on('request', function (swoole_http_request $request, swoole_http_response $response) {
    // 得到客户端过来的请求
    // 得到请求的类型
    $method = strtolower($request->server['request_method']);
    // 请求的uri
    $uri = $request->server['request_uri'];
    // 列表请求是要用GET请求的方式
    if ('get' == $method) {
        // 文章列表
        if ('/api/v1/articles' == $uri) {
            $offset = $request->get['offset'] ?? 0;
            $limit  = $request->get['limit'] ?? 10;
            // 操作数据库  协程
            $db = new \Swoole\Coroutine\MySQL();
            $db->connect([
                'host'     => '127.0.0.1',
                'port'     => '3306',
                'charset'  => 'utf8',
                'user'     => 'root',
                'password' => 'admin888',
                'database' => 'test'
            ]);
            // 编写sql
            $sql = "select * from articles limit $offset,$limit";
            $ret = $db->query($sql);
            // 加上头信息
            $response->header('Content-Type', 'application/json;charset=utf-8');
            $response->end(json_encode($ret,JSON_UNESCAPED_UNICODE));
        }
    }
});
// 启动服务
$http->start();Swoole接口
相关推荐
- 
                            生成图片from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con 查看详情
- 
                            获取指定目录下的所有图片信息1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach ( 查看详情
- 
                            Thinkphp各版本的PHP要求ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3 查看详情
 智享笔记
								    智享笔记								 
                             
                             
                            