Created
September 21, 2017 06:06
-
-
Save suconghou/c28e06a158efe93f2836d5c745ce35dd to your computer and use it in GitHub Desktop.
php stream proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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