承接 torres-developer/reverse-proxy 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

torres-developer/reverse-proxy

Composer 安装命令:

composer require torres-developer/reverse-proxy

包简介

Reverse Proxy using only PHP

README 文档

README

A reverse proxy with PHP.

Why?

My school gives to the students access to a server.

We can serve static files and PHP it's also supported. That is why this is in PHP.

Let's say I created an HTTP server at localhost port 3000 (with PHP, node.js, ...). I can't make people able to access it over the internet because I don't have privileges to change the Apache server config or whatever to create a reverse proxy.

I'm trying to use the tools that were given to me :)

Examples

The simplest use

It will reverse proxy to http://localhost:3000/ always.

<?php

declare(encoding="UTF-8");
declare(strict_types=1);

use function TorresDeveloper\ReverseProxy\reverse_proxy;

require __DIR__ . "/vendor/autoload.php";

reverse_proxy("http://localhost:3000/");

Other Example

In this example in contrary to the other one the request does not always go to http://localhost:3000/. You need to make a request to the path /app/ and then what appears after the /app/ will also be part of the request path that the reverse proxy request so a request to /app/something/ will do a request to http://localhost:3000/something/.

I'm also showing how you can handle some \Exceptions and the use off the function respond to show respond the status code, headers, body, to the client.

<?php

declare(encoding="UTF-8");
declare(strict_types=1);

use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Client\RequestExceptionInterface;
use TorresDeveloper\HTTPMessage\Response;
use TorresDeveloper\HTTPMessage\URI;
use TorresDeveloper\ReverseProxy\ReverseProxy;

use function TorresDeveloper\ReverseProxy\respond;
use function TorresDeveloper\ReverseProxy\serverRequest;

require __DIR__ . "/vendor/autoload.php";

$proxy = new ReverseProxy("/app/", new URI("http://localhost:3000/"));

try {
    $res = $proxy->sendRequest(serverRequest());
} catch (RequestExceptionInterface $e) {
    $method = $e->getRequest()->getMethod();
    $uri = $e->getRequest()->getUri();
    respond(new Response(
        500,
        body: "Request `[$method] $uri` failed.",
        headers: [
            "Content-Type" => "text/plain"
        ]
    ));
} catch (NetworkExceptionInterface $e) {
    $method = $e->getRequest()->getMethod();
    $uri = $e->getRequest()->getUri();
    respond(new Response(
        500,
        body: "Request `[$method] $uri` could not be completed because of network issues.",
        headers: [
            "Content-Type" => "text/plain"
        ]
    ));
} catch (ClientExceptionInterface $e) {
    respond(new Response(
        500,
        body: "Unexpected error occured on the reverse proxy side.",
        headers: [
            "Content-Type" => "text/plain"
        ]
    ));
} catch (\Throwable $th) {
    respond(new Response(
        500,
        body: "Unexpected error occured.",
        headers: [
            "Content-Type" => "text/plain"
        ]
    ));
}

if ($res->getStatusCode() === 404) {
    // It might be that the request to possibly reverse proxy didn't start with
    // the path of the endpoint defined on the ReverseProxy::__constructor
    // earlier in the code.
}

respond($res);

exit(0);

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2023-04-21