请问如果在cli模式下启用http协议,如何发送header头呢?

tangzq
<?php 
require_once __DIR__ . '/vendor/autoload.php'; 
use Workerman\Worker; 
$http_worker = new Worker("http://0.0.0.0:2345");
$http_worker->onConnect = function ($connection) {
    $connection->protocol = '\\Workerman\\Protocols\\Http'; 
}; 
\Workerman\Protocols\Http::header('Content-Type:application/json; charset=utf-8'); 
$http_worker->onWorkerStart = function ($worker) {
// 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
    global $db;
    $db = new \Workerman\MySQL\Connection('127.0.0.1', '50681', 'root', 'root', 'aqr_develop');
}; 
// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function ($connection, $data) {
    // 通过全局变量获得db实例
    global $db;
    // 执行SQL
    $all_tables = $db->query('show tables');
    $connection->send(json_encode($all_tables));
    // $connection->send(PHP_SAPI);
}; 
// 运行worker
Worker::runAll();

设置了header头,无效

3549 2 0
2个回答

walkor 打赏

\Workerman\Protocols\Http::header('Content-Type:application/json; charset=utf-8'); $connection->send(json_encode($all_tables)); 
 
调用header后再调用send

  • tangzq 2019-04-18

    好的,已经成功了。谢谢

ijasonjiang

请问下,你这边是怎么设置header,我按照还是不可以

截图


$io->on('workerStart', function () {
    global $httpWorkUrl;
    // 监听一个http端口
    $inner_http_worker = new Worker($httpWorkUrl);
    // 当http客户端发来数据时触发
    $inner_http_worker->onMessage = function ($http_connection, $data) {
        global $clientIds, $io;
        $_POST = $_POST ? $_POST : $_GET;
        echo date('Y-m-d H:i:s') . 'workerStart::: ' . var_export($_POST, true) . PHP_EOL;
        if (isset($_POST['type'])) {
            global $db, $dbConfig;
            $db = new \Medoo\Medoo($dbConfig);
            $type = $_POST['type'];
            switch ($type) {
                case 'chat_group_messages':
                    $roomId = $_POST['room_id'];
                    $limit = $_POST['limit'] ?? 10;
                    $where = [
                        'room_id[=]' => $roomId,
                        'ORDER'      => ['id' => 'DESC'],
                        "LIMIT"      => $limit,
                    ];
                    if (!empty($_POST['id'])) {
                        $where['id[<]'] = $_POST['id'];
                    }
                    $messages = $db->select('users_messages', '*', $where);
                    $data = json_encode([
                        'status' => 1,
                        'limit' => $limit,
                        'list' => $messages,
                    ]);
                    // $http_connection->setHeader('Content-Type', 'application/json');
                    \Workerman\Protocols\Http::header('Content-Type:application/json; charset=utf-8');
                    return $http_connection->send($data);
                    break;
                default:
                    # code...
                    break;
            }
            return $http_connection->send(json_encode(['satus'=>0]));
        } 
    };
    // 执行监听
    $inner_http_worker->listen();
});

if (!defined('GLOBAL_START')) {
    Worker::runAll();
}```
  • 暂无评论
年代过于久远,无法发表回答
×
🔝