欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解PHP實現(xiàn)HTTP服務(wù)器過程

 更新時間:2023年02月15日 16:05:27   作者:PHP武器庫  
一般來講,PHP很少談到“實現(xiàn)HTTP服務(wù)”的說法,因為從早期的CGI到后來的PHP-FPM,官方已經(jīng)給出了最穩(wěn)定的HTTP解決方案,你只要配合一個Apache或Nginx類的服務(wù)器就能實現(xiàn)穩(wěn)定的HTTP服務(wù)

PHP并非不能實現(xiàn)HTTP服務(wù),一般來講,這叫網(wǎng)絡(luò)編程或Socket編程。在學(xué)習(xí)到其他語言的這部分的時候,一般的思路就是如何監(jiān)聽TCP實現(xiàn)一個服務(wù)器,并處理HTTP協(xié)議。

PHP也可以這樣做,同時一般伴隨著高性能這樣的關(guān)鍵字出現(xiàn)。

原生Socket編程

我們可以通過PHP的Socket函數(shù),很簡單的實現(xiàn)出HTTP服務(wù)。

function run()
{
    //創(chuàng)建服務(wù)端的socket套接流,net協(xié)議為IPv4,protocol協(xié)議為TCP
    $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
    /*綁定接收的套接流主機和端口,與客戶端相對應(yīng)*/
    if(socket_bind($socket,"0.0.0.0", 9502) == false){
        echo 'server bind fail:'.socket_strerror(socket_last_error());exit();
    }
    //監(jiān)聽套接流
    if(socket_listen($socket,4)==false){
        echo 'server listen fail:'.socket_strerror(socket_last_error());exit();
    }
    //非阻塞
    socket_set_nonblock($socket);
    call_user_func('onAccept',$socket);
}
run();

然后通過Socket處理收到的數(shù)據(jù)以及作出響應(yīng):

function onMessage($connection)
{
    //拼裝返回的html內(nèi)容
    $content = '<html><title>hello,world</title><body>hello,world,http</body></html>';
    //拼裝頭信息
    $header = '';
    $header .= "HTTP/1.1 200 OK\r\n";
    $header .= "Date: ".gmdate('D, d M Y H:i:s T')."\r\n";
    $header .= "Content-Type: text/html;charset=utf-8\r\n";
    $header .= "Content-Length: ".strlen($content)."\r\n\r\n";//必須2個\r\n表示頭部信息結(jié)束
    $header .= $content;
    socket_write($connection,$header,strlen($header));
}
function onAccept($socket)
{
    //接收客戶端傳遞過來的信息
    while(true)
    {
        $accept_resource = socket_accept($socket);
        if($accept_resource !== false)
        {
            $string = socket_read($accept_resource,1024);
            echo 'server receive is :'.$string.PHP_EOL;
            if($string != false)
            {
                call_user_func('onMessage',$accept_resource);
            }
        }
    }
}

流行項目

實際上,PHP有很多在項目都在實現(xiàn)HTTP服務(wù)器,而且他們一般也都宣稱是高性能的。

Workerman系

Workerman是一款純PHP開發(fā)的開源高性能的PHP 應(yīng)用容器。幾乎能夠?qū)崿F(xiàn)任何類型的網(wǎng)絡(luò)編程,并且內(nèi)置了一個HTTP協(xié)議。

$worker = new Worker('http://0.0.0.0:1221');

Workerman的官方在21年出品了Webman,一個基于Workerman實現(xiàn)的高性能HTTP服務(wù)框架。替代傳統(tǒng)PHP-FPM架構(gòu),提供高性能的HTTP服務(wù)??梢杂脕黹_發(fā)網(wǎng)站、接口、微服務(wù)。

Webman實際上是一個開發(fā)框架,項目的目錄結(jié)構(gòu)都已經(jīng)設(shè)定好了,按照文檔開發(fā)就行,最后只要通過命令就能運行起來。

php start.php start

Webman支持是一個MVC框架,支持命名空間自動加載,所以代碼像這樣:

<?php
namespace app\controller;
use support\Request;
class UserController
{
    public function hello(Request $request)
    {
        $default_name = 'webman';
        // 從get請求里獲得name參數(shù),如果沒有傳遞name參數(shù)則返回$default_name
        $name = $request->get('name', $default_name);
        // 向瀏覽器返回字符串
        return response('hello ' . $name);
    }
}

除了高性能等特點,他的上手難度很低,并且風(fēng)格與現(xiàn)代的MVC風(fēng)格一致,支持PSR標準,代碼精簡高效。如果你是ThinkPHP的開發(fā)者,你會發(fā)現(xiàn)很容易上手Webman。

Swoole系

說道高性能HTTP服務(wù),總是繞不開swoole的,他也是國內(nèi)最早火熱起來的PHP高性能解決方案。

使用swoole實現(xiàn)HTTP服務(wù)的代碼也很簡單:

$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->on('Request', function ($request, $response) {
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
$http->start();

swoole實際上是一個PHP的擴展,近幾年基于他發(fā)展起了很多的高性能框架,比如easyswoole、Hyperf、Swoft、MixPHP等等。它們都基于Swoole實現(xiàn)框架,可以很容易的創(chuàng)建完整度很成熟的系統(tǒng)。

ReactPHP系

ReactPHP 是用于 PHP 事件驅(qū)動編程的底層庫。也可以用來實現(xiàn)各類網(wǎng)絡(luò)編程,包括HTTP服務(wù)。用它實現(xiàn)HTTP服務(wù)也很簡單:

require __DIR__ . '/vendor/autoload.php';
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
    return React\Http\Message\Response::plaintext(
        "Hello World!\n"
    );
});
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$http->listen($socket);
echo "Server running at http://127.0.0.1:8080" . PHP_EOL;

它是一個底層庫,一般而言,所有PSR的框架都可以基于他運行,替換PHP-FPM。所以他也提供了各個流行框架的接入方案,包括laravel、symfony等,基于ReactPHP,開發(fā)了一個PHP-PM項目。

PHP-PM 是 PHP 應(yīng)用程序的進程管理器、增壓器和負載平衡器。

可以直接通過命令運行:

ppm start --bootstrap=laravel --app-env=prod --debug=0 --logging=0 --workers=20

實際上ReactPHP是個很有趣的項目,比如IP電視服務(wù)器、終端shell、Mqtt的server、PHP版的Redis、一個GUI框架、比特幣P2P網(wǎng)絡(luò)等等,以后有機會給大家介紹介紹。

AMPHP系

AMPHP 是 PHP 的高質(zhì)量、事件驅(qū)動庫的集合,在設(shè)計時考慮了纖維和并發(fā)性。

基于AMPHP實現(xiàn)的HTTP服務(wù)框架叫amphp/http-server。使用它也可以快速實現(xiàn)一個穩(wěn)定高性能的HTTP服務(wù)。

use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\SocketHttpServer;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Amp\Http\Status;
use Amp\Socket\Server;
use Psr\Log\NullLogger;
// Run this script, then visit http://localhost:1337/ in your browser.
Amp\Loop::run(function () {
    $sockets = [
        Server::listen("0.0.0.0:1337"),
        Server::listen("[::]:1337"),
    ];
    $server = new SocketHttpServer($sockets, new ClosureRequestHandler(function (Request $request) {
        return new Response(Status::OK, [
            "content-type" => "text/plain; charset=utf-8"
        ], "Hello, World!");
    }), new NullLogger);
    yield $server->start();
    // Stop the server gracefully when SIGINT is received.
    // This is technically optional, but it is best to call Server::stop().
    Amp\Loop::onSignal(SIGINT, function (string $watcherId) use ($server) {
        Amp\Loop::cancel($watcherId);
        yield $server->stop();
    });
});

AMPHP也實現(xiàn)了很多有趣的項目,比如Mysql的客戶端,能夠?qū)崿F(xiàn)連接池等特性。

swow

swow是一個基于協(xié)程的跨平臺并發(fā)I/O引擎,關(guān)注并發(fā)IO。

官方給出的HTTP例子代碼行數(shù)比較多,主要是展示了HTTP請求支持的每個階段的操作方法,代碼也是很簡潔的。

declare(strict_types=1);
use Swow\Buffer;
use Swow\Coroutine;
use Swow\Http\Parser;
use Swow\Http\ParserException;
use Swow\Socket;
use Swow\SocketException;
$host = getenv('SERVER_HOST') ?: '127.0.0.1';
$port = (int) (getenv('SERVER_PORT') ?: 9764);
$backlog = (int) (getenv('SERVER_BACKLOG') ?: 8192);
$multi = (bool) (getenv('SERVER_MULTI') ?: false);
$bindFlag = Socket::BIND_FLAG_NONE;
$server = new Socket(Socket::TYPE_TCP);
if ($multi) {
    $server->setTcpAcceptBalance(true);
    $bindFlag |= Socket::BIND_FLAG_REUSEPORT;
}
$server->bind($host, $port, $bindFlag)->listen($backlog);
while (true) {
    try {
        $connection = $server->accept();
    } catch (SocketException $exception) {
        break;
    }
    Coroutine::run(static function () use ($connection): void {
        $buffer = new Buffer(Buffer::COMMON_SIZE);
        $parser = (new Parser())->setType(Parser::TYPE_REQUEST)->setEvents(Parser::EVENT_BODY);
        $parsedOffset = 0;
        $body = null;
        try {
            while (true) {
                $length = $connection->recv($buffer, $buffer->getLength());
                if ($length === 0) {
                    break;
                }
                while (true) {
                    $parsedOffset += $parser->execute($buffer, $parsedOffset);
                    if ($parser->getEvent() === $parser::EVENT_NONE) {
                        $buffer->truncateFrom($parsedOffset);
                        $parsedOffset = 0;
                        break; /* goto recv more data */
                    }
                    if ($parser->getEvent() === Parser::EVENT_BODY) {
                        $body ??= new Buffer(Buffer::COMMON_SIZE);
                        $body->write(0, $buffer, $parser->getDataOffset(), $parser->getDataLength());
                    }
                    if ($parser->isCompleted()) {
                        $response = sprintf(
                            "HTTP/1.1 200 OK\r\n" .
                            "Connection: %s\r\n" .
                            "Content-Length: %d\r\n\r\n" .
                            '%s',
                            $parser->shouldKeepAlive() ? 'Keep-Alive' : 'Closed',
                            $body ? $body->getLength() : 0,
                            $body ?: ''
                        );
                        $connection->send($response);
                        $body?->clear();
                        break; /* goto recv more data */
                    }
                }
                if (!$parser->shouldKeepAlive()) {
                    break;
                }
            }
        } catch (SocketException $exception) {
            echo "No.{$connection->getFd()} goaway! {$exception->getMessage()}" . PHP_EOL;
        } catch (ParserException $exception) {
            echo "No.{$connection->getFd()} parse error! {$exception->getMessage()}" . PHP_EOL;
        }
        $connection->close();
    });
}

總結(jié)

以上是一些非常流行的PHP框架和項目,但還有其他很多實現(xiàn)了高性能HTTP服務(wù)的項目。這里不多做介紹了。雖然我們談到PHP的時候,很少談到網(wǎng)絡(luò)編程,甚至在入門教程中根本就沒有網(wǎng)絡(luò)編程這節(jié)課。但是使用PHP做網(wǎng)絡(luò)編程的各項應(yīng)用已經(jīng)很火熱了。

在入門其他語言是一定有一節(jié)課程是學(xué)習(xí)網(wǎng)絡(luò)編程的,做PHP教程的也應(yīng)該考慮考慮增加這部分課程了。

到此這篇關(guān)于詳解PHP實現(xiàn)HTTP服務(wù)器過程的文章就介紹到這了,更多相關(guān)PHP HTTP服務(wù)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論