Skip to content

Instantly share code, notes, and snippets.

@suconghou
Created September 21, 2017 06:06
Show Gist options
  • Save suconghou/c28e06a158efe93f2836d5c745ce35dd to your computer and use it in GitHub Desktop.
Save suconghou/c28e06a158efe93f2836d5c745ce35dd to your computer and use it in GitHub Desktop.
php stream proxy
<?php
class stream
{
private static $ignore=['Host'];
public static function init($url)
{
$headers = [];
foreach(getallheaders() as $k => $v)
{
if(!in_array($k,self::$ignore,true))
{
$headers[]="{$k}:$v";
}
}
$opts =
[
'http'=>
[
'protocol_version'=>1.1,
'method'=>$_SERVER['REQUEST_METHOD'],
'header'=>implode("\r\n",$headers),
'content'=>file_get_contents('php://input')
]
];
$r=stream_context_create($opts);
$remote = fopen($url, 'r',false,$r);
$output = fopen('php://output','w');
if($output&&$remote)
{
foreach($http_response_header as $h)
{
header($h);
}
return stream_copy_to_stream($remote,$output);
}
}
}
if(!empty($_GET['u']) && filter_var($_GET['u'],FILTER_VALIDATE_URL))
{
stream::init($_GET['u']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment