国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

說明

workerman從4.x版本開始加強了HTTP服務的支持。引入了請求類、響應類、session類以及SSE。如果你想使用workerman的HTTP服務,強烈推薦使用workerman4.x或者以后的更高版本。

注意以下都是workerman4.x版本的用法,不兼容workerman3.x。

獲得請求對象

請求對象一律在onMessage回調(diào)函數(shù)中獲取,框架會自動將Request對象通過回調(diào)函數(shù)第二個參數(shù)傳遞進來。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    // $request為請求對象,這里沒有對請求對象執(zhí)行任何操作直接返回hello給瀏覽器
    $connection->send("hello");
};

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

當瀏覽器訪問http://127.0.0.1:8080時將返回hello。

獲得請求參數(shù)get

獲取整個get數(shù)組

$get = $request->get();

如果請求沒有g(shù)et參數(shù)則返回一個空的數(shù)組。

獲取get數(shù)組的某一個值

$name = $request->get('name');

如果get數(shù)組中不包含這個值則返回null。

你也可以給get方法第二個參數(shù)傳遞一個默認值,如果get數(shù)組中沒找到對應值則返回默認值。例如:

$name = $request->get('name', 'tom');

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->get('name'));
};

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

當瀏覽器訪問http://127.0.0.1:8080?name=jerry&age=12時將返回jerry。

獲得請求參數(shù)post

獲取整個post數(shù)組

$post = $request->post();

如果請求沒有post參數(shù)則返回一個空的數(shù)組。

獲取post數(shù)組的某一個值

$name = $request->post('name');

如果post數(shù)組中不包含這個值則返回null。

與get方法一樣,你也可以給post方法第二個參數(shù)傳遞一個默認值,如果post數(shù)組中沒找到對應值則返回默認值。例如:

$name = $request->post('name', 'tom');

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $post = $request->post();
    $connection->send(var_export($post, true));
};

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

獲得原始請求post包體

$post = $request->rawBody();

這個功能類似與 php-fpm里的 file_get_contents("php://input");操作。用于獲得http原始請求包體。這在獲取非application/x-www-form-urlencoded格式的post請求數(shù)據(jù)時很有用。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $post = json_decode($request->rawBody());
    $connection->send('hello');
};

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

獲取header

獲取整個header數(shù)組

$headers = $request->header();

如果請求沒有header參數(shù)則返回一個空的數(shù)組。注意所有key均為小寫。

獲取header數(shù)組的某一個值

$host = $request->header('host');

如果header數(shù)組中不包含這個值則返回null。注意所有key均為小寫。

與get方法一樣,你也可以給header方法第二個參數(shù)傳遞一個默認值,如果header數(shù)組中沒找到對應值則返回默認值。例如:

$host = $request->header('host', 'localhost');

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    if ($request->header('connection') === 'keep-alive') {
        $connection->send('hello');
    } else {
        $connection->close('hello');
    }    
};

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

獲取cookie

獲取整個cookie數(shù)組

$cookies = $request->cookie();

如果請求沒有cookie參數(shù)則返回一個空的數(shù)組。

獲取cookie數(shù)組的某一個值

$name = $request->cookie('name');

如果cookie數(shù)組中不包含這個值則返回null。

與get方法一樣,你也可以給cookie方法第二個參數(shù)傳遞一個默認值,如果cookie數(shù)組中沒找到對應值則返回默認值。例如:

$name = $request->cookie('name', 'tom');

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $cookie = $request->cookie();
    $connection->send(var_export($cookie, true));
};

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

獲取上傳文件

獲取整個上傳文件數(shù)組

$files = $request->file();

返回的文件格式類似:

array (
    'avatar' => array (
            'name' => '123.jpg',
            'tmp_name' => '/tmp/workerman.upload.9hjR4w',
            'size' => 1196127,
            'error' => 0,
            'type' => 'application/octet-stream',
      ),
     'anotherfile' =>  array (
            'name' => '456.txt',
            'tmp_name' => '/tmp/workerman.upload.9sirSws',
            'size' => 490,
            'error' => 0,
            'type' => 'text/plain',
      )
)

其中:

  • name為文件名字
  • tmp_name為磁盤臨時文件位置
  • size為文件大小
  • error為錯誤碼
  • type為文件mine類型。

注意:

  • 上傳文件大小受到defaultMaxPackageSize限制,默認10M,可修改。

  • 請求結(jié)束后文件將被自動清除。

  • 如果請求沒有上傳文件則返回一個空的數(shù)組。

獲取特定上傳文件

$avatar_file = $request->file('avatar');

返回類似

array (
        'name' => '123.jpg',
        'tmp_name' => '/tmp/workerman.upload.9hjR4w',
        'size' => 1196127,
        'error' => 0,
        'type' => 'application/octet-stream',
  )

如果上傳文件不存在則返回null。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $file = $request->file('avatar');
    if ($file && $file['error'] === UPLOAD_ERR_OK) {
        rename($file['tmp_name'], '/home/www/web/public/123.jpg');
        $connection->send('ok');
        return;
    }
    $connection->send('upload fail');
};

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

獲取host

獲取請求的host信息。

$host = $request->host();

如果請求的地址是非標準的80或者443端口,host信息可能會攜帶端口,例如example.com:8080。如果不需要端口第一個參數(shù)可以傳入true

$host = $request->host(true);

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->host());
};

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

當瀏覽器訪問http://127.0.0.1:8080?name=tom時將返回127.0.0.1:8080。

獲取請求方法

$method = $request->method();

返回值可能是GETPOST、PUT、DELETE、OPTIONSHEAD中的一個。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->method());
};

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

獲取請求uri

$uri = $request->uri();

返回請求的uri,包括path和queryString部分。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->uri());
};

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

當瀏覽器訪問http://127.0.0.1:8080/user/get.php?uid=10&type=2時將返回/user/get.php?uid=10&type=2。

獲取請求路徑

$path = $request->path();

返回請求的path部分。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->path());
};

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

當瀏覽器訪問http://127.0.0.1:8080/user/get.php?uid=10&type=2時將返回/user/get.php。

獲取請求queryString

$query_string = $request->queryString();

返回請求的queryString部分。

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->queryString());
};

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

當瀏覽器訪問http://127.0.0.1:8080/user/get.php?uid=10&type=2時將返回uid=10&type=2。

獲取請求HTTP版本

$version = $request->protocolVersion();

返回字符串 1.1 或者1.0

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->protocolVersion());
};

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

獲取請求sessionId

$sid = $request->sessionId();

返回字符串,由字母和數(shù)字組成

例子

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:8080');

$worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send($request->sessionId());
};

// 運行worker
Worker::runAll();
編輯于2024-03-13 17:37:18 完善本頁 +發(fā)起討論
贊助商