当前位置:首页 > CN2资讯 > 正文内容

简单的php代理 Simple PHP Proxyphp proxy

1天前CN2资讯



index.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" language="javascript"> // I want to use json2.js because it allows me to format stringified JSON with // pretty indents, so let's nuke any existing browser-specific JSON parser. window.JSON = null; </script> <script type="text/javascript" src="/js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="http://benalman.com/code/projects/php-simple-proxy/shared/json2.js"></script> <script> (function($){$(document).ready(function(e) { // Handle form submit. $('#params').submit(function(){ var proxy = 'proxy.php', url = proxy + '?' + $('#params').serialize(); // Update some stuff. $('#request').html( $('<a/>').attr( 'href', url ).text( url ) ); $('#response').html( 'Loading...' ); // Test to see if HTML mode. if ( /mode=native/.test( url ) ) { // Make GET request. $.get( url, function(data) { $('#response') .html( '<pre />' ) .find( 'pre' ) .text( data ); }); } else { // Make JSON request. $.getJSON( url, function(data) { $('#response') .html( '<pre />' ) .find( 'pre' ) .text( JSON.stringify( data, null, 2 ) ); }); } // Prevent default form submit action. return false; }); // Submit the form on page load if ?url= is passed into the example page. if ( $('#url').val() !== '' ) $('#params').submit(); // Disable AJAX caching. $.ajaxSetup({ cache: false }); // Disable dependent checkboxes as necessary. $('input:radio').click(function(){ var that = $(this), c1 = 'dependent-' + that.attr('name'), c2 = c1 + '-' + that.val(); that.closest('form') .find( '.' + c1 + ' input' ) .attr( 'disabled', 'disabled' ) .end() .find( '.' + c2 + ' input' ) .removeAttr( 'disabled' ); }); // Clicking sample remote urls should populate the "Remote URL" box. $('#sample a').click(function(){ $('#url').val( $(this).attr( 'href' ) ); return false; }); });})($staff) </script> <form action="" method="get" id="params"> <div> <label> <b>Remote URL</b> <input type="text" value="" name="url" id="url" /> </label> </div> <p id="sample"> ..or try these sample Remote URLs: <a href="http:///">GitHub</a>, <a href="http:///cowboy/php-simple-proxy/raw/master/examples/simple/json_sample.js">a sample JSON (not JSONP) request</a>, <a href="http:///omg404errorpage">a 404 error page</a> </p> <div> <label> <input type="radio" disabled="disabled" value="native" name="mode"> Native <i>(disabled by default)</i> </label> </div> <div> <label> <input type="radio" disabled="disabled" checked="checked" value="json" name="mode"> JSON </label> </div> <div > <div> <label> <input type="checkbox" checked="checked" value="1" name="full_headers"> Full Headers </label> </div> <div> <label> <input type="checkbox" checked="checked" value="1" name="full_status"> Full Status </label> </div> </div> <input type="submit" value="Submit" name="submit" > </form> <h3>Request URL</h3> <p id="request">N/A, click Submit!</p> <h3>Simple PHP Proxy response</h3> <div id="response">N/A, click Submit!</div>

 

proxy.php

<?PHP // Script: Simple PHP Proxy: Get external HTML, JSON and more! // // *Version: 1.6, Last updated: 1/24/2009* // // Project Home - http://benalman.com/projects/php-simple-proxy/ // GitHub - http:///cowboy/php-simple-proxy/ // Source - http:///cowboy/php-simple-proxy/raw/master/ba-simple-proxy.php // // About: License // // Copyright (c) 2010 "Cowboy" Ben Alman, // Dual licensed under the MIT and GPL licenses. // http://benalman.com/about/license/ // // About: Examples // // This working example, complete with fully commented code, illustrates one way // in which this PHP script can be used. // // Simple - http://benalman.com/code/projects/php-simple-proxy/examples/simple/ // // About: Release History // // 1.6 - (1/24/2009) Now defaults to JSON mode, which can now be changed to // native mode by specifying ?mode=native. Native and JSONP modes are // disabled by default because of possible XSS vulnerability issues, but // are configurable in the PHP script along with a url validation regex. // 1.5 - (12/27/2009) Initial release // // Topic: GET Parameters // // Certain GET (query string) parameters may be passed into ba-simple-proxy.php // to control its behavior, this is a list of these parameters. // // url - The remote URL resource to fetch. Any GET parameters to be passed // through to the remote URL resource must be urlencoded in this parameter. // mode - If mode=native, the response will be sent using the same content // type and headers that the remote URL resource returned. If omitted, the // response will be JSON (or JSONP). <Native requests> and <JSONP requests> // are disabled by default, see <Configuration Options> for more information. // callback - If specified, the response JSON will be wrapped in this named // function call. This parameter and <JSONP requests> are disabled by // default, see <Configuration Options> for more information. // user_agent - This value will be sent to the remote URL request as the // `User-Agent:` HTTP request header. If omitted, the browser user agent // will be passed through. // send_cookies - If send_cookies=1, all cookies will be forwarded through to // the remote URL request. // send_session - If send_session=1 and send_cookies=1, the SID cookie will be // forwarded through to the remote URL request. // full_headers - If a JSON request and full_headers=1, the JSON response will // contain detailed header information. // full_status - If a JSON request and full_status=1, the JSON response will // contain detailed cURL status information, otherwise it will just contain // the `http_code` property. // // Topic: POST Parameters // // All POST parameters are automatically passed through to the remote URL // request. // // Topic: JSON requests // // This request will return the contents of the specified url in JSON format. // // Request: // // > ba-simple-proxy.php?url=http://example.com/ // // Response: // // > { "contents": "<html>...</html>", "headers": {...}, "status": {...} } // // JSON object properties: // // contents - (String) The contents of the remote URL resource. // headers - (Object) A hash of HTTP headers returned by the remote URL // resource. // status - (Object) A hash of status codes returned by cURL. // // Topic: JSONP requests // // This request will return the contents of the specified url in JSONP format // (but only if $enable_jsonp is enabled in the PHP script). // // Request: // // > ba-simple-proxy.php?url=http://example.com/&callback=foo // // Response: // // > foo({ "contents": "<html>...</html>", "headers": {...}, "status": {...} }) // // JSON object properties: // // contents - (String) The contents of the remote URL resource. // headers - (Object) A hash of HTTP headers returned by the remote URL // resource. // status - (Object) A hash of status codes returned by cURL. // // Topic: Native requests // // This request will return the contents of the specified url in the format it // was received in, including the same content-type and other headers (but only // if $enable_native is enabled in the PHP script). // // Request: // // > ba-simple-proxy.php?url=http://example.com/&mode=native // // Response: // // > <html>...</html> // // Topic: Notes // // * Assumes magic_quotes_gpc = Off in php.ini // // Topic: Configuration Options // // These variables can be manually edited in the PHP file if necessary. // // $enable_jsonp - Only enable <JSONP requests> if you really need to. If you // install this script on the same server as the page you're calling it // from, plain JSON will work. Defaults to false. // $enable_native - You can enable <Native requests>, but you should only do // this if you also whitelist specific URLs using $valid_url_regex, to avoid // possible XSS vulnerabilities. Defaults to false. // $valid_url_regex - This regex is matched against the url parameter to // ensure that it is valid. This setting only needs to be used if either // $enable_jsonp or $enable_native are enabled. Defaults to '/.*/' which // validates all URLs. // // ############################################################################ // Change these configuration options if needed, see above descriptions for info. $enable_jsonp = false; $enable_native = false; $valid_url_regex = '/.*/'; // ############################################################################ $url = $_GET['url']; if ( !$url ) { // Passed url not specified. $contents = 'ERROR: url not specified'; $status = array( 'http_code' => 'ERROR' ); } else if ( !preg_match( $valid_url_regex, $url ) ) { // Passed url doesn't match $valid_url_regex. $contents = 'ERROR: invalid url'; $status = array( 'http_code' => 'ERROR' ); } else { $ch = curl_init( $url ); if ( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) { curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST ); } if ( $_GET['send_cookies'] ) { $cookie = array(); foreach ( $_COOKIE as $key => $value ) { $cookie[] = $key . '=' . $value; } if ( $_GET['send_session'] ) { $cookie[] = SID; } $cookie = implode( '; ', $cookie ); curl_setopt( $ch, CURLOPT_COOKIE, $cookie ); } curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $ch, CURLOPT_HEADER, true ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_USERAGENT, $_GET['user_agent'] ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'] ); list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 ); $status = curl_getinfo( $ch ); curl_close( $ch ); } // Split header text into an array. $header_text = preg_split( '/[\r\n]+/', $header ); if ( $_GET['mode'] == 'native' ) { if ( !$enable_native ) { $contents = 'ERROR: invalid mode'; $status = array( 'http_code' => 'ERROR' ); } // Propagate headers to response. foreach ( $header_text as $header ) { if ( preg_match( '/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header ) ) { header( $header ); } } print $contents; } else { // $data will be serialized into JSON data. $data = array(); // Propagate all HTTP headers into the JSON data object. if ( $_GET['full_headers'] ) { $data['headers'] = array(); foreach ( $header_text as $header ) { preg_match( '/^(.+?):\s+(.*)$/', $header, $matches ); if ( $matches ) { $data['headers'][ $matches[1] ] = $matches[2]; } } } // Propagate all cURL request / response info to the JSON data object. if ( $_GET['full_status'] ) { $data['status'] = $status; } else { $data['status'] = array(); $data['status']['http_code'] = $status['http_code']; } // Set the JSON data object contents, decoding it from JSON if possible. $decoded_json = json_decode( $contents ); $data['contents'] = $decoded_json ? $decoded_json : $contents; // Generate appropriate content-type header. $is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) ); // Get JSONP callback. $jsonp_callback = $enable_jsonp && isset($_GET['callback']) ? $_GET['callback'] : null; // Generate JSON/JSONP string $json = json_encode( $data ); print $jsonp_callback ? "$jsonp_callback($json)" : $json; }

 


 

 

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/26303.html

    分享给朋友:

    “简单的php代理 Simple PHP Proxyphp proxy” 的相关文章

    VPS主机如何选择?灵活性与性价比并存的最佳方案

    在当今信息化高速发展的时代,VPS主机成为了许多企业和个人用户的热门选择。那么,什么是VPS主机呢?它是通过虚拟化技术在一台物理服务器上创建的多个独立服务器。每个虚拟专用服务器(VPS)都具备自己的操作系统、CPU、内存和存储空间,用户可以像管理独立服务器一样灵活配置和控制自己的VPS,真是个便利的...

    SpartanHost VPS主机评测:高性能与安全性的理想选择

    在我开始探索VPS主机市场时,SpartanHost引起了我的注意。这个公司成立于2013年,自那时起便在行业中扎根,专注于提供高性能的VPS解决方案。他们使用的是基于KVM架构的主机产品,充分满足用户的需求。从他们的运营历史来看,尽管时间不算很久,但SpartanHost凭借其稳定的服务和灵活的选...

    VPSDime评测:高性价比的VPS服务选择

    VPSDime概述 在如今互联网发展的浪潮中,各种主机服务商层出不穷,VPSDime作为一家成立于2013年的海内外主机服务商,引起了我的关注。它隶属于Nodisto IT,专注于VPS业务,提供多种类型的虚拟专用服务器。这对我这样的用户来说,选择合适的主机服务显得尤为重要,尤其是对于需要高性能和高...

    提升科研效率:1536微量高速离心机及其应用

    产品概述与特点 在实验室的工作中,设备的效率通常会直接影响到实验的结果。1536微量高速离心机就是这样一款能够大大提高离心效率的设备。它能够处理1.5ml和2.0ml的离心管、8连管、PCR管以及5ml管,极大地方便了科学研究中的样品处理流程。产品的设计充分考虑了用户的使用需求,具备了最高15,00...

    购买DNS解锁服务器的最佳选择与配置指南

    在当今的信息时代,获取我们想要的内容常常并不像想象中那样简单。很多流媒体服务在不同地区的可用性有所限制,这使得我们在享受内容时常常受到阻碍。这时候,DNS解锁服务器就成为了解决这个问题的有效工具。DNS解锁技术通过修改服务器上的DNS设置,可以帮助用户突破地理限制,顺利访问各种国际流媒体服务。 我刚...

    怎么在VPS上测速并提升网络性能

    在使用虚拟专用服务器(VPS)时,了解它的性能和网络速度是至关重要的。VPS的效率直接影响到网站的加载速度和用户体验。想想当你的网站访问速度慢,用户可能会不耐烦,从而导致访客流失。这可不是任何网站主想要发生的事情。所以,定期对VPS进行测速,找出潜在问题,并加以解决,是一个非常明智的选择。 测速不仅...