Help us understand the problem. What is going on with this article?

PHPでプロキシサーバーを作る

More than 1 year has passed since last update.

PHP書いてたらプロキシ鯖を書きたくなることってありますよね。

composer require guzzlehttp/guzzle

index.php
<?php

namespace zonuexe\ZoProxy;

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7;

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

// ここはいい感じにやってね
$host_table = [
    'hoge.example.com' => [
        'host' => 'localhost',
        'port' => 3939,
        'scheme' => 'http',
    ],
    'foo.example.com' => [
        'host' => 'foo.example.jp',
        'scheme' => 'https',
    ],
];

$request = Psr7\ServerRequest::fromGlobals();
$new_uri = $request->getUri()->withPort(80);

$key = $new_uri->getHost();
$port = $new_uri->getPort();
if ($port !== null && !Psr7\Uri::isDefaultPort($port)) {
    $key .= ":{$port}";
}

if (isset($host_table[$key])) {
    if (isset($host_table[$key]['host'])) {
        $new_uri = $new_uri->withHost($host_table[$key]['host']);
    }
    if (isset($host_table[$key]['port'])) {
        $new_uri = $new_uri->withPort($host_table[$key]['port']);
    }
    if (isset($host_table[$key]['scheme'])) {
        $new_uri = $new_uri->withScheme($host_table[$key]['scheme']);
    }
}

$client = new HttpClient;
$response = $client->send($request->withUri($new_uri), [
    'http_errors' => false,
]);

foreach ($response->getHeaders() as $key => $values) {
    foreach ($values as $value) {
        header("{$key}:{$value}");
    }
}

echo $response->getBody();

まあ普通にやったらこれ以上は簡単にならないだろって感じなので、著作権とかは特に気にしないで勝手にコピペして良いです。

Q. 何の役に立つの…?

A. さあ… 僕はこれを書いてから仕事で活用しまくりですが

例としては :80 にPHP5系が動いてるApacheが立ってる共用の開発鯖でPHP 7.1のビルトインサーバーを php -S localhost:3939 で起動して、そこにプロキシするみたいなことができますね。

みそとしては、HTTPのHostを変更してないので私の場合はそれで都合良かったのですが、たぶんそれだとまづいことの方が多いので、そこはよしなにやってください ヾ(〃><)ノ゙

tadsan
僕に警備する自宅をください。Emacs初心者。Rubyist。 全ての投稿された記事は別段の表記がない限りはCC 3.0 BY-SA https://creativecommons.org/licenses/by-sa/3.0/deed.ja で二次利用できます。 記事中に含まれる全てのコードスニペットの著作権は抛棄するので、煮るなり焼くなりお好きにどうぞ。
https://tadsan.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした