{"id":21372664,"url":"https://github.com/torres-developer/php-reverse-proxy","last_synced_at":"2025-03-16T08:44:58.117Z","repository":{"id":153897361,"uuid":"630973731","full_name":"torres-developer/php-reverse-proxy","owner":"torres-developer","description":"Reverse Proxy with PHP","archived":false,"fork":false,"pushed_at":"2023-04-23T00:29:24.000Z","size":48,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-22T21:14:45.420Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/torres-developer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-21T15:40:21.000Z","updated_at":"2023-04-21T17:13:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"a41dece0-14ec-4199-8253-4a5e373d5730","html_url":"https://github.com/torres-developer/php-reverse-proxy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torres-developer%2Fphp-reverse-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torres-developer%2Fphp-reverse-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torres-developer%2Fphp-reverse-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/torres-developer%2Fphp-reverse-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/torres-developer","download_url":"https://codeload.github.com/torres-developer/php-reverse-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243846975,"owners_count":20357297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-22T08:22:02.107Z","updated_at":"2025-03-16T08:44:58.060Z","avatar_url":"https://github.com/torres-developer.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reverse Proxy\n\nA reverse proxy with PHP.\n\n# Why?\n\nMy school gives to the students access to a server.\n\nWe can serve static files and PHP it's also supported. That is why this is in\nPHP.\n\nLet's say I created an HTTP server at localhost port 3000 (with PHP, node.js,\n...). I can't make people able to access it over the internet because I don't\nhave privileges to change the Apache server config or whatever to create a\nreverse proxy.\n\nI'm trying to use the tools that were given to me :)\n\n# Examples\n\n## The simplest use\n\nIt will reverse proxy to `http://localhost:3000/` always.\n\n``` php\n\u003c?php\n\ndeclare(encoding=\"UTF-8\");\ndeclare(strict_types=1);\n\nuse function TorresDeveloper\\ReverseProxy\\reverse_proxy;\n\nrequire __DIR__ . \"/vendor/autoload.php\";\n\nreverse_proxy(\"http://localhost:3000/\");\n```\n\n## Other Example\n\nIn this example in contrary to the other one the request does not always go to\n`http://localhost:3000/`. You need to make a request to the path `/app/` and\nthen what appears after the `/app/` will also be part of the request path that\nthe reverse proxy request so a request to `/app/something/` will do a request\nto `http://localhost:3000/something/`.\n\nI'm also showing how you can handle some `\\Exception`s and the use off the\nfunction `respond` to show respond the status code, headers, body, to the\nclient.\n\n``` php\n\u003c?php\n\ndeclare(encoding=\"UTF-8\");\ndeclare(strict_types=1);\n\nuse Psr\\Http\\Client\\ClientExceptionInterface;\nuse Psr\\Http\\Client\\NetworkExceptionInterface;\nuse Psr\\Http\\Client\\RequestExceptionInterface;\nuse TorresDeveloper\\HTTPMessage\\Response;\nuse TorresDeveloper\\HTTPMessage\\URI;\nuse TorresDeveloper\\ReverseProxy\\ReverseProxy;\n\nuse function TorresDeveloper\\ReverseProxy\\respond;\nuse function TorresDeveloper\\ReverseProxy\\serverRequest;\n\nrequire __DIR__ . \"/vendor/autoload.php\";\n\n$proxy = new ReverseProxy(\"/app/\", new URI(\"http://localhost:3000/\"));\n\ntry {\n    $res = $proxy-\u003esendRequest(serverRequest());\n} catch (RequestExceptionInterface $e) {\n    $method = $e-\u003egetRequest()-\u003egetMethod();\n    $uri = $e-\u003egetRequest()-\u003egetUri();\n    respond(new Response(\n        500,\n        body: \"Request `[$method] $uri` failed.\",\n        headers: [\n            \"Content-Type\" =\u003e \"text/plain\"\n        ]\n    ));\n} catch (NetworkExceptionInterface $e) {\n    $method = $e-\u003egetRequest()-\u003egetMethod();\n    $uri = $e-\u003egetRequest()-\u003egetUri();\n    respond(new Response(\n        500,\n        body: \"Request `[$method] $uri` could not be completed because of network issues.\",\n        headers: [\n            \"Content-Type\" =\u003e \"text/plain\"\n        ]\n    ));\n} catch (ClientExceptionInterface $e) {\n    respond(new Response(\n        500,\n        body: \"Unexpected error occured on the reverse proxy side.\",\n        headers: [\n            \"Content-Type\" =\u003e \"text/plain\"\n        ]\n    ));\n} catch (\\Throwable $th) {\n    respond(new Response(\n        500,\n        body: \"Unexpected error occured.\",\n        headers: [\n            \"Content-Type\" =\u003e \"text/plain\"\n        ]\n    ));\n}\n\nif ($res-\u003egetStatusCode() === 404) {\n    // It might be that the request to possibly reverse proxy didn't start with\n    // the path of the endpoint defined on the ReverseProxy::__constructor\n    // earlier in the code.\n}\n\nrespond($res);\n\nexit(0);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorres-developer%2Fphp-reverse-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftorres-developer%2Fphp-reverse-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorres-developer%2Fphp-reverse-proxy/lists"}