主頁(yè) > 知識(shí)庫(kù) > PHP swoole中http_server的配置與使用方法實(shí)例分析

PHP swoole中http_server的配置與使用方法實(shí)例分析

熱門標(biāo)簽:電子圍欄 團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 服務(wù)器配置 銀行業(yè)務(wù)

本文實(shí)例講述了PHP swoole中http_server的配置與使用方法。分享給大家供大家參考,具體如下:

swoole中為我們提供了一個(gè)swoole_http_server類,方便我們處理http請(qǐng)求。

但是它對(duì)http協(xié)議的支持并不完整,所以一般建議在前面加一層nginx進(jìn)行代理,對(duì)于php文件的處理交由swoole處理。

一、創(chuàng)建一個(gè)簡(jiǎn)單的http服務(wù)

?php
//創(chuàng)建一個(gè)http server服務(wù)
$server = new swoole_http_server('0.0.0.0', 8888);
 
$server->set([
  'package_max_length' => 1024 * 1024 * 10,
  //設(shè)置文件上傳的臨時(shí)目錄
  'upload_tmp_dir' => __DIR__ . '/uploads/',
]);
 
//設(shè)置request事件回調(diào)
//函數(shù)有兩個(gè)參數(shù):
//swoole_http_request對(duì)象,負(fù)責(zé)http請(qǐng)求相關(guān)信息
//swoole_http_response對(duì)象,負(fù)責(zé)向客戶端響應(yīng)相關(guān)信息
$server->on('request', function (swoole_http_request $request, swoole_http_response $response) {
  //請(qǐng)求的頭部信息
  var_dump($request->header);
 
  //請(qǐng)求相關(guān)的服務(wù)器信息,相當(dāng)于PHP中的$_SERVER
  var_dump($request->server);
 
  //請(qǐng)求的GET參數(shù),相當(dāng)于PHP中的$_GET
  var_dump($request->get);
 
  //請(qǐng)求的POST參數(shù),相當(dāng)于PHP中的$_POST
  var_dump($request->post);
 
  //請(qǐng)求的COOKIE信息
  var_dump($request->cookie);
 
  //文件上傳信息,文件大小不超過package_max_length的值
  var_dump($request->files);
 
  //獲取原始POST請(qǐng)求數(shù)據(jù),相當(dāng)于fopen('php://input');
  var_dump($request->rawContent());
 
  //獲取完整http請(qǐng)求報(bào)文
  var_dump($request->getData());
 
  //向客戶端發(fā)送信息
  $response->end('hello');
});
 
//啟動(dòng)服務(wù)
$server->start();

二、處理靜態(tài)文件

swoole中已經(jīng)幫我們內(nèi)置了兩個(gè)配置參數(shù),只需要簡(jiǎn)單配置一下就可以實(shí)現(xiàn)。

不過功能簡(jiǎn)易,不建議外網(wǎng)使用,有需求的可以自已實(shí)現(xiàn)。

?php
$server = new swoole_http_server('0.0.0.0', 8888);
 
$server->set([
  //配置靜態(tài)文件根目錄
  'document_root' => __DIR__ . '/statics/',
  //開啟靜態(tài)文件請(qǐng)求處理功能,這樣當(dāng)請(qǐng)求的是一個(gè)靜態(tài)文件時(shí),swoole自動(dòng)會(huì)在上面配置的目錄中查找并返回
  'enable_static_handler' => true,
]);
 
$server->on('request', function ($request, $response) {
 
});
 
$server->start();

三、處理文件上傳

?php
//創(chuàng)建一個(gè)http server服務(wù)
$server = new swoole_http_server('0.0.0.0', 8888);
 
$server->set([
  //文件上傳大小不超過該值
  'package_max_length' => 1024 * 1024 * 50,
  //設(shè)置文件上傳的臨時(shí)目錄
  'upload_tmp_dir' => __DIR__ . '/tmp/',
  'upload_dir' => __DIR__ . '/uploads/',
  'document_root' => __DIR__ . '/statics/',
  'enable_static_handler' => true,
]);
 
$server->on('request', function ($request, $response) use ($server) {
  if ($request->server['path_info'] == '/upload') {
    $tmp = $request->files['upload']['tmp_name'];
    $upload = $server->setting['upload_dir'] . $request->files['upload']['name'];
    if (file_exists($tmp) 
      move_uploaded_file($tmp, $upload)) {
      $response->header('Content-Type', 'text/html; charset=UTF-8');
      $response->end('上傳成功');
    } else {
      $response->end('上傳失敗');
    }
  }
});
 
//啟動(dòng)服務(wù)
$server->start();

我們?cè)趕tatics目錄下創(chuàng)建一個(gè)upload.html文件:

!doctype html>
html lang="zh-CN">
head>
  meta charset="UTF-8">
  title>文件上傳/title>
/head>
body>
form action="/upload" method="post" enctype="multipart/form-data">
  input type="file" name="upload" value="">
  input type="submit" value="提交">
/form>
/body>
/html>

四、處理路由文件自動(dòng)加載

?php
//創(chuàng)建一個(gè)http server服務(wù)
$server = new swoole_http_server('0.0.0.0', 8888);
 
$server->set([
  //配置項(xiàng)目的目錄
  'project_path' => __DIR__ . '/src/',
]);
 
$server->on('WorkerStart', function ($server, $worker_id) {
  //注冊(cè)自動(dòng)加載函數(shù)
  spl_autoload_register(function ($class) use($server) {
    $class = $server->setting['project_path'] . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
 
    if (file_exists($class)) {
      include_once $class;
    }
  });
});
 
$server->on('request', function ($request, $response) use ($server) {
  $pathInfo = explode('/', ltrim($request->server['path_info'], '/'));
 
  //模塊/控制器/方法
  $module = $pathInfo[0] ?? 'Index';
  $controller = $pathInfo[1] ?? 'Index';
  $method = $pathInfo[2] ?? 'index';
 
  try {
    $class = "\\{$module}\\{$controller}";
    $result = (new $class)->{$method}();
    $response->end($result);
  } catch (\Throwable $e) {
    $response->end($e->getMessage());
  }
});
 
//啟動(dòng)服務(wù)
$server->start();

我們?cè)谀夸?src 下創(chuàng)建 test 目錄,并創(chuàng)建 test.php 文件

?php
namespace Test;
 
class Test
{
  public function test()
  {
    return 'test';
  }
}

然后訪問 127.0.0.1:8888/test/test/test 就可以看到返回結(jié)果了。

通過$request->server['path_info'] 來(lái)找到模塊,控制器,方法,然后注冊(cè)我們自已的加載函數(shù),引入文件。實(shí)例化類對(duì)象,然后調(diào)用方法,返回結(jié)果。

注意,不要將 spl_autoload_register 放到 onStart 事件回調(diào)函數(shù)中。

onStart 回調(diào)中,僅允許echo、打印Log、修改進(jìn)程名稱。不得執(zhí)行其他操作。

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php socket用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計(jì)算法總結(jié)》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • 詳解PHP Swoole長(zhǎng)連接常見問題
  • 詳解PHP Swoole與TCP三次握手
  • 詳解PHP7開啟OPcache和Swoole性能的提升對(duì)比
  • php中Swoole的熱更新實(shí)現(xiàn)代碼實(shí)例
  • windows系統(tǒng)php環(huán)境安裝swoole具體步驟
  • php使用Swoole實(shí)現(xiàn)毫秒級(jí)定時(shí)任務(wù)的方法
  • php使用goto實(shí)現(xiàn)自動(dòng)重啟swoole、reactphp、workerman服務(wù)的代碼
  • PHP swoole的process模塊創(chuàng)建和使用子進(jìn)程操作示例
  • PHP swoole中使用task進(jìn)程異步的處理耗時(shí)任務(wù)應(yīng)用案例分析
  • 淺談swoole的作用與原理

標(biāo)簽:江蘇 廣元 衡水 大理 衢州 蚌埠 萍鄉(xiāng) 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP swoole中http_server的配置與使用方法實(shí)例分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266