Skip to content

Instantly share code, notes, and snippets.

@Wowfunhappy
Forked from iovar/proxy.php
Last active July 13, 2020 21:58
Show Gist options
  • Save Wowfunhappy/67e0c1d72d0714367c589e563873ddb7 to your computer and use it in GitHub Desktop.
Save Wowfunhappy/67e0c1d72d0714367c589e563873ddb7 to your computer and use it in GitHub Desktop.
Simple PHP Proxy Script
<?
/**
* A simple PHP proxy.
* Based on https://gist.github.com/iovar/9091078
* Modified by Wowfunhappy
*
* Usage: http://path/to/proxy.php?example.com
*/
$url = $_SERVER["QUERY_STRING"];
if ((substr($url, 0, 7) !== 'http://') && (substr($url, 0, 8) !== 'https://'))
{
$url = 'http://' . $url;
}
$method = $_SERVER['REQUEST_METHOD'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
if( $method !== "GET") {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
if( $method == "PUT" || $method == "PATCH" || ($method == "POST" && empty($_FILES))) {
$data_str = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
}
elseif($method == "POST") {
$data_str = array();
if(!empty($_FILES)) {
foreach ($_FILES as $key => $value) {
$full_path = realpath( $_FILES[$key]['tmp_name']);
$data_str[$key] = '@'.$full_path;
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str+$_POST);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
$headers = getallheaders();
$headers_str = [];
foreach ( $headers as $key => $value){
if($key == 'Host')
continue;
$headers_str[]=$key.":".$value;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_str );
$result = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
header('Content-Type: ' . $content_type);
echo $result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment