Skip to content

Instantly share code, notes, and snippets.

@codler
Created May 1, 2012 14:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codler/2568564 to your computer and use it in GitHub Desktop.
Save codler/2568564 to your computer and use it in GitHub Desktop.
Simple PHP Proxy
<?php
/**
* @author Han Lin Yap < http://zencodez.net/ >
* @copyright 2012 zencodez.net
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @package proxy
* @version 1.0 - 2012-05-01
*/
$method = $_SERVER['REQUEST_METHOD'];
$request = substr($_SERVER['PATH_INFO'], 1);
$request = str_replace('http:/', 'http://', $request);
# Request headers
$headers = apache_request_headers();
$header_string = '';
foreach ($headers AS $header => $value) {
if (strtolower($header) == 'host') continue;
$header_string .= "$header: $value\r\n";
}
$opts = array('http' =>
array(
'method' => $method,
'header' => $header_string,
)
);
# Request content
if ($method == 'GET') {
$request .= '?' . http_build_query($_GET);
} else {
$content = ($method == 'POST') ? http_build_query($_POST) : @file_get_contents('php://input');
$opts['http']['content'] = $content;
}
# Proxy
$result = @file_get_contents($request, false, stream_context_create($opts));
# Response headers
foreach ($http_response_header AS $header) {
header($header);
}
# Response content
echo $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment