Repository navigation
Expand file tree
/
Copy pathserver.php
More file actions
65 lines (55 loc) · 1.72 KB
/
Copy pathserver.php
File metadata and controls
65 lines (55 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* PHP based Proxy.
*
* This is small but fast and simple proxy server
* based on React and Guzzle, use and modify / contribute at will
*
* PHP version 5,7
*
* @category Proxy
*
* @copyright 2016 Jack Trefon Media Group
* @author Jacek Trefon <jack@trefon.com>
* @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.en.html
*
* @link https://github.com/jtrefon
*/
require_once 'vendor/autoload.php';
use React\EventLoop\Factory;
use React\Socket\Server as SocketServer;
use React\Http\Server as HttpServer;
use React\Http\Request;
use React\Http\Response;
use GuzzleHttp\Client;
$app = function (Request $request, Response $response) {
$config = include 'config.php';
$query = $request->getQuery();
$path = $config['destinationUrl'].$request->getPath();
if (count($query) > 0) {
$param = http_build_query($query);
$path = $config['destinationUrl'].$request->getPath().'?'.$param;
}
try {
$guzzleClient = new Client();
$guzzleResponse = $guzzleClient->request(
$request->getMethod(),
$path,
$request->getHeaders()
);
$headers = $guzzleResponse->getHeaders();
$headers['Access-Control-Allow-Origin'] = $config['corsOrigin'];
$response->writeHead($guzzleResponse->getStatusCode(), $headers);
$response->end($guzzleResponse->getBody());
} catch (\Exception $e) {
echo "\nError: ".$e->getMessage();
}
};
$loop = Factory::create();
$socket = new SocketServer($loop);
$http = new HttpServer($socket);
$http->on('request', $app);
$config = include 'config.php';
$socket->listen($config['port']);
echo 'server is now listening for calls on port: '.$config['port'];
$loop->run();