实例:http://www.ikeepstudying.com/tools/proxy/

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 class="brush:xml"/>' ).find( 'pre' ).text( data );});} else {// Make JSON request.$.getJSON( url, function(data){$('#response').html( '<pre class="brush:js"/>' ).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" class="text" id="url" style="width:90%" /></label></div><p id="sample">..or try these sample Remote URLs:<a href="http://github.com/">GitHub</a>,<a href="http://github.com/cowboy/php-simple-proxy/raw/master/examples/simple/json_sample.js">a sample JSON (not JSONP) request</a>,<a href="http://github.com/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 class="dependent-mode dependent-mode-json indent"><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" class="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://github.com/cowboy/php-simple-proxy/
// Source       - http://github.com/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;}

实例:http://www.ikeepstudying.com/tools/proxy/

原文:http://benalman.com/projects/php-simple-proxy/

简单的php代理 Simple PHP Proxy相关推荐

  1. 【java项目实战】代理模式(Proxy Pattern),静态代理 VS 动态代理

    这篇博文,我们主要以类图和代码的形式来对照学习一下静态代理和动态代理.重点解析各自的优缺点. 定义 代理模式(Proxy Pattern)是对象的结构型模式,代理模式给某一个对象提供了一个代理对象,并 ...

  2. JAVA设计模式(09):结构型-代理模式(Proxy)

    代理模式是经常使用的结构型设计模式之中的一个,当无法直接訪问某个对象或訪问某个对象存在困难时能够通过一个代理对象来间接訪问,为了保证client使用的透明性,所訪问的真实对象与代理对象须要实现同样的接 ...

  3. 轻松学,Java 中的代理模式(proxy)及动态代理

    我们先来分析代理这个词. 代理 代理是英文 Proxy 翻译过来的.我们在生活中见到过的代理,大概最常见的就是朋友圈中卖面膜的同学了. 她们从厂家拿货,然后在朋友圈中宣传,然后卖给熟人. 按理说,顾客 ...

  4. Java提高班(六)反射和动态代理(JDK Proxy和Cglib)

    反射和动态代理放有一定的相关性,但单纯的说动态代理是由反射机制实现的,其实是不够全面不准确的,动态代理是一种功能行为,而它的实现方法有很多.要怎么理解以上这句话,请看下文. 一.反射 反射机制是 Ja ...

  5. Java接口学习(接口的使用、简单工厂、代理模式、接口和抽象类的区别)

    前言引入 官方解释:Java接口是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能). 我的 ...

  6. 一个简单 JDK 动态代理的实例

    动态代理的步骤: 创建一个实现了 InvocationHandler 接口的类,必须重写接口里的 invoke()方法. 创建被代理的类和接口 通过 Proxy 的静态方法 newProxyInsat ...

  7. Android常见设计模式——代理模式(Proxy Pattern)(二)

    文章目录 1. 前言 2. 远程代理(Remote Proxy) 3. 后记 1. 前言 在上篇Android常见设计模式--代理模式(Proxy Pattern)中基本上知道了什么是代理模式,以及对 ...

  8. Android常见设计模式——代理模式(Proxy Pattern)

    文章目录 1. 前言 2. 代理模式(Proxy Pattern) 2.1 静态代理模式 2.2 动态代理模式 3. Android 中的代理模式 3.1 Retrofit中的代理模式(没有被代理者) ...

  9. Java设计模式-----Cglib动态代理(Cglib Proxy)

    接上文:4.2Java设计模式-----JDK动态代理(Dynamic Proxy) Cglib动态代理 百度百科:Cglib是一个强大的,高性能,高质量的Code生成类库,它可以在运行期扩展Java ...

最新文章

  1. Protractor测试环境搭建
  2. android设备未指定怎么办,APKpath未指定为模块“示例 – 示例”
  3. .NET 4.0有一个新的GAC,为什么?
  4. Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)
  5. 【CentOS Linux 7】实验2【Linux多用户管理】
  6. 【Mark 常用方法】Html中<form>标签作用和属性详解
  7. python实例 71,72
  8. 基于jQuery的uploadify(flash上传文件)控件v1.6.2 bug修正
  9. ASP.NETMVC Model验证(五)
  10. 【蓝牙学习笔记】Arduino设置蓝牙模块HC-06 CC2540 CC2541自动初始化
  11. windows系统搭建图像识别开发环境
  12. 智能优化算法:樽海鞘群优化算法-附代码
  13. 语义错误和语法错误的区别
  14. 《Java并发编程的艺术》——锁(笔记)
  15. 搜索 阿虚同学_凉宫春日阿虚台词“在虚构的故事当中寻求真实感的人脑袋一定有问题”动画是出自那一集?...
  16. Kali新安装时软件安装及配置[自用 欢迎补充]
  17. 一张图30分钟带你入门python-我,30分钟,P了100张图,秒杀全公司同事
  18. 论文笔记:Meta-attention for ViT-backed Continual Learning CVPR 2022
  19. 华为1+X认证网络系统管理与运维中级实验
  20. python 画高程图像

热门文章

  1. python3 random模块_Python3 中 random模块
  2. qtwebengineprocess已停止工作_windows资源管理器总是停止工作
  3. centos boot dvd版本_iPad版Photoshop来了,设计师可以躺着改稿了!!!
  4. 安装torch_sparse失败解决方法
  5. linux那些事之 page translation(硬件篇)
  6. OpenCV中基本数据结构(5)_RotatedRect
  7. 浅谈OpenCL之Platform API(1)
  8. 使用PyCharm连接云主机教程
  9. java steam filter 动态条件_Filter解决全站编码问题
  10. link标签引入.css文件(目的):适配不同屏幕