关于workerman处理http协议$_SERVER变量问题

zzlasp

请教一下,最新版的workerman处理http请求时不再支持$_SERVER变量了吗?之前在3.5.31版的中onMessage处理函数中是可以的,而且是传统意义上的server变量,但在最新版中得到的却是与服务器本身有关的一些参数,在最新版中有什么替代方案?

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';

// 创建一个Worker监听2345端口,使用http协议通讯
$http_worker = new Worker("http://0.0.0.0:8000");

// 启动4个进程对外提供服务
$http_worker->count = 4;

// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function(TcpConnection $connection, Request $request)
{
    var_dump($_SERVER);
    //...
};

// 运行worker
Worker::runAll();

workerman v3.5.31结果:

array (
  'QUERY_STRING' => '',
  'REQUEST_METHOD' => 'GET',
  'REQUEST_URI' => '/',
  'SERVER_PROTOCOL' => 'HTTP/1.1',
  'SERVER_SOFTWARE' => 'workerman/3.5.31',
  'SERVER_NAME' => '192.168.33.10',
  'HTTP_HOST' => '192.168.33.10:8000',
  'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
  'HTTP_ACCEPT' => 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
  'HTTP_ACCEPT_LANGUAGE' => 'zh-CN,zh;q=0.9',
  'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
  'HTTP_COOKIE' => 'thinkphp_show_page_trace=1|1',
  'HTTP_CONNECTION' => 'keep-alive',
  'CONTENT_TYPE' => '',
  'REMOTE_ADDR' => '192.168.33.1',
  'REMOTE_PORT' => 5187,
  'SERVER_PORT' => '8000',
  'HTTP_PRAGMA' => 'no-cache',
  'HTTP_CACHE_CONTROL' => 'no-cache',
  'HTTP_REFERER' => 'http://192.168.33.10:8000/',
)

workerman v4.1.3得到结果:

array (                                                                                                                        
  'XDG_SESSION_ID' => '187',                                                                                                   
  'HOSTNAME' => 'centos7.localdomain',                                                                                         
  'SHELL' => '/bin/bash',                                                                                                      
  'TERM' => 'cygwin',                                                                                                          
  'HISTSIZE' => '100000',                                                                                                      
  'SSH_CLIENT' => '10.0.2.2 1855 22',                                                                                          
  'SSH_TTY' => '/dev/pts/0',                                                                                                   
  'USER' => 'vagrant',                                                                                                         
  'LS_COLORS' => 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su
=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=
01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz
=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.de
b=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;3
1:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=
01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35
:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.q
t=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:
*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01
;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.a
xa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:',                                                                               
  'PATH' => '/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin',                    
  'MAIL' => '/var/spool/mail/vagrant',                                                                                         
  'PWD' => '/laraman/think',                                                                                                   
  'LANG' => 'en_US.UTF-8',                                                                                                     
  'HISTCONTROL' => 'ignoredups',                                                                                               
  'HOME' => '/root',                                                                                                           
  'SHLVL' => '2',                                                                                                              
  'LOGNAME' => 'vagrant',                                                                                                      
  'SSH_CONNECTION' => '10.0.2.2 1855 10.0.2.15 22',                                                                            
  'LESSOPEN' => '||/usr/bin/lesspipe.sh %s',                                                                                   
  'XDG_RUNTIME_DIR' => '/run/user/1000',                                                                                       
  '_' => '/usr/bin/php',                                                                                                       
  'OLDPWD' => '/laraman/thinkman',                                                                                             
  'PHP_SELF' => 'think',                                                                                                       
  'SCRIPT_NAME' => 'think',                                                                                                    
  'SCRIPT_FILENAME' => 'think',                                                                                                
  'PATH_TRANSLATED' => 'think',                                                                                                
  'DOCUMENT_ROOT' => '',                                                                                                       
  'REQUEST_TIME_FLOAT' => 1665027653.3169019,                                                                                  
  'REQUEST_TIME' => 1665027653,                                                                                                
  'argv' =>                                                                                                                    
  array (                                                                                                                      
    0 => 'think',                                                                                                              
    1 => 'wk',                                                                                                                 
    2 => 'start',                                                                                                              
  ),                                                                                                                           
  'argc' => 3,                                                                                                                 
)
1271 1 0
1个回答

six

onMessage里自己设置下应该可以,请求数据要么在$request里取,要么在$request->header()里

$http_worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $_SERVER = [
        'QUERY_STRING' => $request->queryString(),
        'REQUEST_METHOD' => $request->method(),
        'REQUEST_URI' => $request->uri(),
        'HTTP_HOST' => $request->header('host'),
        // ......
    ];
};
  • zzlasp 2022-10-06

    谢谢指点,看来也只能先这样处理了

年代过于久远,无法发表回答
×
🔝