• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

用PHP实现反向代理服务器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Part1:代码实例

  1 <?php 
  2 set_time_limit(60);
  3 if( !defined('__DIR__') )
  4 {
  5   define('__DIR__',dirname(__FILE__)) ;
  6 }
  7 
  8 $_REQUEST['url'] =gtRootUrl();
  9 //改成网站正式服务器ip
 10 $ip= '127.0.0.1';
 11 $aAccess = curl_init() ;
 12 // --------------------
 13 // set URL and other appropriate options
 14 curl_setopt($aAccess, CURLOPT_URL, $_REQUEST['url']);
 15 curl_setopt($aAccess, CURLOPT_HEADER, true);
 16 curl_setopt($aAccess, CURLOPT_RETURNTRANSFER, true);
 17 curl_setopt($aAccess, CURLOPT_FOLLOWLOCATION, false);
 18 curl_setopt($aAccess, CURLOPT_SSL_VERIFYPEER, false);  
 19 curl_setopt($aAccess, CURLOPT_SSL_VERIFYHOST, false);  
 20 curl_setopt($aAccess, CURLOPT_TIMEOUT, 60);
 21 curl_setopt($aAccess, CURLOPT_BINARYTRANSFER, true);
 22 //curl_setopt($aAccess, CURLOPT_HTTPPROXYTUNNEL, 0);
 23 curl_setopt($aAccess,CURLOPT_PROXY,$ip.':80');
 24 //curl_setopt($aAccess,CURLOPT_PROXY,'127.0.0.1:8888');
 25 
 26 if(!empty($_SERVER['HTTP_REFERER']))
 27     curl_setopt($aAccess,CURLOPT_REFERER,$_SERVER['HTTP_REFERER']) ;
 28 
 29 
 30 
 31 $headers=get_client_header();
 32 curl_setopt($aAccess,CURLOPT_HTTPHEADER,$headers) ;
 33 
 34 if( $_SERVER['REQUEST_METHOD']=='POST' )
 35 {
 36     curl_setopt($aAccess, CURLOPT_POST, 1);
 37     curl_setopt($aAccess, CURLOPT_POSTFIELDS, http_build_query($_POST));
 38 }
 39 
 40 
 41 // grab URL and pass it to the browser
 42 
 43 $sResponse = curl_exec($aAccess);
 44 list($headerstr,$sResponse)=parseHeader($sResponse);
 45 $headarr= explode("\r\n", $headerstr);
 46 foreach($headarr as $h){
 47     if(strlen($h)>0){
 48         if(strpos($h,'Content-Length')!==false) continue;
 49         if(strpos($h,'Transfer-Encoding')!==false) continue;
 50         if(strpos($h,'Connection')!==false) continue;
 51         if(strpos($h,'HTTP/1.1 100 Continue')!==false) continue;
 52         header($h);
 53     }
 54 }
 55 
 56 function replace_html_path($arrMatche)
 57 {    
 58     $sPath = makeUrl($arrMatche[4]) ;
 59     if( strtolower($arrMatche[1])=='img' )
 60     {
 61         $sPath.= '&bin=1' ;
 62     }
 63     
 64     return "<{$arrMatche[1]} {$arrMatche[2]} {$arrMatche[3]}=\"{$sPath}\"" ;
 65 }
 66 
 67 function get_client_header(){
 68     $headers=array();
 69     foreach($_SERVER as $k=>$v){
 70         if(strpos($k,'HTTP_')===0){
 71             $k=strtolower(preg_replace('/^HTTP/', '', $k));
 72             $k=preg_replace_callback('/_\w/','header_callback',$k);
 73             $k=preg_replace('/^_/','',$k);
 74             $k=str_replace('_','-',$k);
 75             if($k=='Host') continue;
 76             $headers[]="$k:$v";
 77         }
 78     }
 79     return $headers;
 80 }
 81 
 82 function header_callback($str){
 83     return strtoupper($str[0]);
 84 }
 85 
 86 function parseHeader($sResponse){
 87     list($headerstr,$sResponse)=explode("\r\n\r\n",$sResponse, 2);
 88     $ret=array($headerstr,$sResponse);
 89     if(preg_match('/^HTTP\/1\.1 \d{3}/', $sResponse)){
 90         $ret=parseHeader($sResponse);
 91     }
 92     return $ret;
 93 }
 94 
 95 function gtRootUrl()
 96 {
 97 //缓存结果,同一个request不重复计算
 98  static $gtrooturl;
 99  if(empty($gtrooturl)){
100     // Protocol
101     $s = !isset($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'on') ? 's' : '';
102     $protocol = strtolower($_SERVER['SERVER_PROTOCOL']);
103     $protocol = substr($protocol,0,strpos($protocol,'/')).$s.'://';
104     // Port
105     $port = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
106     // Server name
107     $server_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'].$port : getenv('SERVER_NAME').$port;
108     // Host
109     $host = isset($_SERVER['HTTP_HOST']) ? strtolower($_SERVER['HTTP_HOST']) : $server_name;
110 
111      $gtrooturl=$protocol.$host.$_SERVER['REQUEST_URI'];
112     }
113         return $gtrooturl;
114 }
115 
116 // close cURL resource, and free up system resources
117 curl_close($aAccess);
118 echo $sResponse ;
View Code 

思想参考原网址

https://my.oschina.net/jamesren/blog/668495


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php7.3_swoole4.4_swoft2.0环境安装发布时间:2022-07-10
下一篇:
被弃用的php函数以及被那个代替发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap