<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[PHP代理]用php实现proxy功能的示范及 PHP CURL 模拟提交（支持代理）]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Tue, 20 Nov 2012 13:31:48 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	用php实现proxy功能的示范<br/>刚刚写的练手的作品。感觉还是有些实用价值的。。放出来给大家试试。<br/>功能：通过php实现代理功能<br/>适用用户：在家下载较慢。且懒得找proxy。且有高速主机，且主机有php，且主机支持socket。<br/>使用方法：<br/>1。把本程序传到主机上<br/>2。通过浏览器浏览主机上的此文件<br/>3。在url中填入欲下载的文件地址（如果有必要，可以填入referer）<br/>4。点击get proxy url<br/>5。新打开的页面中的链接即为通过proxy下载的链接<br/><br/>注意：<br/>1。支持多线程下载,断点续传<br/>2。仅支持http协议<br/>3。不支持服务器端header重定向<br/>4。不支持http auth<br/>5。下载较大的文件时，可以修改程序中set_time_limit的值，以减少断线的现象（默认为180，即3分钟）<br/><br/>PHP代码:<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;?php

/**
 */

/**
 */

/**
 * Php Http Proxy
 */

/**
 * version: 0.0.2
 */

/**
 * last modify: 2005.1.12
 */

/**
 * author: q3boy&lt;q3boy@sina.com&gt;
 */

/**
 */

/**
 */

set_time_limit(180);

define(&#039;PP_ERROR_CODE&#039;, 1);

define(&#039;PP_ERROR_MESSAGE&#039;, 2);

define(&#039;PP_ERROR_ALL&#039;, 3);

define(&#039;PP_RETURN_STRING&#039;, 1);

define(&#039;PP_RETURN_ARRAY&#039;, 2);

define(&#039;PP_RETURN_ALL&#039;, 3);

class CPhpProxy &#123;
&nbsp;&nbsp;var $_strUrl;

&nbsp;&nbsp;var $_arrArgv;

&nbsp;&nbsp;var $_strRef;

&nbsp;&nbsp;var $_arrUrl;

&nbsp;&nbsp;var $_resSocket;

&nbsp;&nbsp;var $_intError;

&nbsp;&nbsp;var $_strError;

&nbsp;&nbsp;var $_arrDefaultArrayUrl;

&nbsp;&nbsp;var $_arrDefaultPort;

&nbsp;&nbsp;function CPhpProxy($strUrl = null, $arrArgv = array()) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; init();

&nbsp;&nbsp;&nbsp;&nbsp;is_null($strUrl)?&#039;&#039;:$this -&gt; setUrl($strUrl);

&nbsp;&nbsp;&nbsp;&nbsp;is_null($arrArgv[&#039;refer&#039;])?&#039;&#039;:$this -&gt; setRef($arrArgv[&#039;refer&#039;]);

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; proxy($strUrl, $arrArgv);
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function setError($intError, $mixArgv = null) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _intError = $intError;

&nbsp;&nbsp;&nbsp;&nbsp;if ($intError == 1001) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strError = &quot;调用 $mixArgv 方法时参数传递出错, 枚举值不存在&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif ($intError == 1) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strError = &quot;url 错误, url 格式不正确.&#92;n&quot; . $mixArgvs;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif ($intError == 2) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strError = &quot;url 错误, 目前尚未支持此协议.&#92;n&quot; . $mixArgvs;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif ($intError == 3) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strError = &quot;无法打开 &quot; . $this -&gt; _arrUrl[&#039;host&#039;] . &#039;:&#039; . $this -&gt; _arrUrl[&#039;port&#039;] . &quot;&#92;n&quot; . $mixArgv[0] . &#039;: &#039; . $mixArgv[1];
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;&nbsp;&nbsp;Return false;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function getError($enumReturnType = PP_ERROR_CODE) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;if ($enumReturnType == PP_ERROR_ALL) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return array(str_pad($this -&gt; _intError, 5, &#039;0&#039;, STR_PAD_LEFT), $this -&gt; _strError);
&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif ($enumReturnType == PP_ERROR_MESSAGE) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; _strError;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif ($enumReturnType == PP_ERROR_CODE) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; _intError;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return false;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function init() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strUrl = null;

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _arrArgv = array();

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strRef = null;

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _arrUrl = array();

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _resSocket = null;

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _intError = 0;

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strError = &#039;there is no error&#039;; 
&nbsp;&nbsp;&nbsp;&nbsp;// arrurl 默认值
&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _arrDefaultArrayUrl = array(&#039;protocol&#039; =&gt; &#039;http&#039;, &#039;file&#039; =&gt; &#039;index.htm&#039;); 
&nbsp;&nbsp;&nbsp;&nbsp;// 默认端口
&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _arrDefaultPort = array(&#039;http&#039; =&gt; &#039;80&#039;);

&nbsp;&nbsp;&nbsp;&nbsp;Return true;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function setUrl($strUrl) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$strUrl = trim($strUrl); 
&nbsp;&nbsp;&nbsp;&nbsp;// 正则
&nbsp;&nbsp;&nbsp;&nbsp;$reg = &quot;/^(([&#92;w]+):&#92;/&#92;/)?([^:&#124;&#92;/&#124;@]*:)?([^:&#124;&#92;/&#124;@]*@)?([&#92;w&#124;&#92;-&#124;&#92;.]+)(:&#92;d+)?(&#92;/[^&#92;?&#124;#]*)?(&#92;?[^#]*)?(#.*)?$/is&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;if (!preg_match($reg, $strUrl, $arrUrl)) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; setError(1, $strUrl);
&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 拆解匹配数组
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list($tmp, $tmp, $arr[&#039;protocol&#039;], $arr[&#039;user&#039;], $arr[&#039;pass&#039;], $arr[&#039;host&#039;], $arr[&#039;port&#039;], $arr[&#039;path&#039;], $arr[&#039;query&#039;], $arr[&#039;anchor&#039;]) = $arrUrl; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 默认协议
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!$arr[&#039;protocol&#039;]) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arrUrl[1] = $this -&gt; _arrDefaultArrayUrl[&#039;protocol&#039;] . &#039;://&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 设 filename
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arr[&#039;file&#039;] = basename($arr[&#039;path&#039;]); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 默认值
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach($this -&gt; _arrDefaultArrayUrl as $key =&gt; $val) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($arr[$key] == &#039;&#039;) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arr[$key] = $val;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 默认端口
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (is_null($this -&gt; _arrDefaultPort[$arr[&#039;protocol&#039;]])) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; setError(2, $arr[&#039;protocol&#039;]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif (!$arr[&#039;port&#039;]) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arr[&#039;port&#039;] = $this -&gt; _arrDefaultPort[$arr[&#039;protocol&#039;]];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 设 uri
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$arr[&#039;uri&#039;] = ($arr[&#039;path&#039;]?$arr[&#039;path&#039;]:&#039;/&#039;) . ($arr[&#039;query&#039;]?&#039;?&#039; . $arr[&#039;query&#039;]:&#039;&#039;) . ($arr[&#039;anchor&#039;]?&#039;#&#039; . $arr[&#039;anchor&#039;]:&#039;&#039;); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 设 url
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset($arrUrl[0]);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unset($arrUrl[2]);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strUrl = implode(&#039;&#039;, $arrUrl); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 设 arrurl
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _arrUrl = $arr;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return true;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function getUrl($enumReturnType = PP_RETURN_ARRAY) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;if ($enumReturnType == PP_RETURN_STRING) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; _strUrl;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; elseif ($enumReturnType == PP_RETURN_ARRAY) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; _arrUrl;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;if ($enumReturnType == PP_RETURN_ALL) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return array($this -&gt; _strUrl, $this -&gt; _arrUrl);
&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; setError(1001);
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function setRefer($strRef) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _strRef = trim($strRef);

&nbsp;&nbsp;&nbsp;&nbsp;Return true;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function getRefer() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; _strRef;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function getProxyUrl() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;global $_SERVER;

&nbsp;&nbsp;&nbsp;&nbsp;list($strProcotol) = explode(&#039;/&#039;, strtolower(trim($_SERVER[&#039;SERVER_PROTOCOL&#039;])));

&nbsp;&nbsp;&nbsp;&nbsp;$str = $strProcotol . &#039;://&#039; . $_SERVER[&#039;HTTP_HOST&#039;] . $_SERVER[&#039;PHP_SELF&#039;] . &#039;?u=&#039; . urlencode($this -&gt; getUrl(PP_RETURN_STRING)) .

&nbsp;&nbsp;&nbsp;&nbsp;($this -&gt; getRefer()?(&#039;&amp;r=&#039; . urlencode($this -&gt; getRefer())):&#039;&#039;);

&nbsp;&nbsp;&nbsp;&nbsp;Return $str;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function openSocket() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$arr = $this -&gt; getUrl();

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _resSocket = @fsockopen($arr[&#039;host&#039;], $arr[&#039;port&#039;], $intError, $strError, 30);

&nbsp;&nbsp;&nbsp;&nbsp;if (!$this -&gt; _resSocket) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _resSocket = null;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; setError(3, array($intError, $strError));
&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return true;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function getRequest() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;get&#039; . ucfirst($this -&gt; _arrUrl[&#039;protocol&#039;]) . &#039;Request&#039;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;();
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function sendRequest() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;Return fwrite($this -&gt; _resSocket, $this -&gt; getRequest());
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function flushResponse() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;Return $this -&gt; &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#039;flush&#039; . ucfirst($this -&gt; _arrUrl[&#039;protocol&#039;]) . &#039;Response&#039;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;();
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function getHttpRequest() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$arr = $this -&gt; getUrl();

&nbsp;&nbsp;&nbsp;&nbsp;$arrRequest = getallheaders();

&nbsp;&nbsp;&nbsp;&nbsp;$arrRequest[&#039;Host&#039;] = $arr[&#039;host&#039;];

&nbsp;&nbsp;&nbsp;&nbsp;$strRequest = &quot;GET &quot; . $arr[&#039;uri&#039;] . &quot; HTTP/1.1&#92;r&#92;n&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;foreach($arrRequest as $key =&gt; $val) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$strRequest .= &quot;$key: $val&#92;r&#92;n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;&nbsp;&nbsp;$strRequest .= &quot;&#92;r&#92;n&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;Return $strRequest;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function flushHttpResponse() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;$bolHeader = true;

&nbsp;&nbsp;&nbsp;&nbsp;while (!feof($this -&gt; _resSocket)) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$str = fgets($this -&gt; _resSocket, 4096);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($bolHeader) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@header($str);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo($str);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($bolHeader &amp;&amp; $str == &quot;&#92;r&#92;n&quot;) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$bolHeader = false;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@header(&quot;Content-type: application/gzip&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@header(&#039;Content-Disposition: attachment; filename=&quot;&#039; . $this -&gt; _arrUrl[&#039;file&#039;] . &#039;&quot;&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;&nbsp;&nbsp;Return true;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function close() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;fclose($this -&gt; _resSocket);

&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; _resSocket = null;

&nbsp;&nbsp;&nbsp;&nbsp;Return true;
&nbsp;&nbsp;&#125; 

&nbsp;&nbsp;function proxy($strUrl = null, $arrArgv = array()) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;if (!is_null($this -&gt; getUrl(PP_RETURN_STRING)) &amp;&amp; $this -&gt; openSocket() &amp;&amp; $this -&gt; sendRequest() &amp;&amp; $this -&gt; flushResponse()) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo(123);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this -&gt; close();
&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return false;
&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&#125; 
&#125; 

if (sizeof($_GET)) &#123;
&nbsp;&nbsp;$strGetArrayName = &#039;_GET&#039;;
&#125; elseif (sizeof($HTTP_GET_VARS)) &#123;
&nbsp;&nbsp;$strGetArrayName = &#039;HTTP_GET_VARS&#039;;
&#125; else &#123;
&nbsp;&nbsp;die(&#039;&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;html&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;head&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;PhpProxy&lt;/title&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;style type=&quot;text/css&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--body,td &#123;font-family: verdana; font-size: 12px;&#125;th,input &#123;font-family: verdana;&#125;--&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;/style&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;/head&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;body&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;form name=&quot;form1&quot; method=&quot;get&quot; action=&quot;&#039; . $_SERVER[&#039;PHP_SELF&#039;] . &#039;&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;table width=&quot;500&quot; border=&quot;1&quot; align=&quot;center&quot; cellpadding=&quot;5&quot; cellspacing=&quot;1&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th colspan=&quot;2&quot;&gt;PhpProxy&lt;/th&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;39&quot; nowrap&gt;URL:&lt;/td&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td width=&quot;432&quot;&gt;&lt;input name=&quot;u&quot; type=&quot;text&quot; id=&quot;u&quot; size=&quot;60&quot;&gt;&lt;/td&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td nowrap&gt;REFERER:&lt;/td&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;input name=&quot;r&quot; type=&quot;text&quot; id=&quot;r&quot; size=&quot;60&quot;&gt;&lt;/td&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;&lt;input name=&quot;act&quot; value=&quot;make&quot; type=&quot;hidden&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Get Proxy Url&quot;&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;reset&quot;&nbsp;&nbsp;value=&quot;Reset&quot;&gt;&lt;/td&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;/body&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;/html&gt;&#039;);
&#125; 

$strUrl = trim($&#123;$strGetArrayName&#125;[&#039;u&#039;]);

if ($strUrl == &#039;&#039;) &#123;
&nbsp;&nbsp;die(&#039;请输入 url 地址.&#039;);
&#125; 
// get referer
$strRefTmp = trim($&#123;$strGetArrayName&#125;[&#039;r&#039;]); 
// 初始化 proxy 类
$objProxy = new CPhpProxy(); 
// 设置 url 和 refer
$objProxy -&gt; setUrl($strUrl);

$objProxy -&gt; setRefer($strRef); 
// 错误输出
if ($objProxy -&gt; getError()) &#123;
&nbsp;&nbsp;die($objProxy -&gt; getError(PP_ERROR_MESSAGE));
&#125; 
// echo url for download
if ($&#123;$strGetArrayName&#125;[&#039;act&#039;] == &#039;make&#039;) &#123;
&nbsp;&nbsp;die(&quot;&lt;center&gt;&lt;h4&gt;&lt;a href=&#039;&quot; . $objProxy -&gt; getProxyUrl() . &quot;&#039;&gt;点击此处通过 PhpProxy 下载文件&lt;/a&gt;&lt;/h2&gt;&lt;/center&gt;&quot;);
&#125; 
// 取文件
$objProxy -&gt; proxy(); 
// 错误处理
if ($objProxy -&gt; getError()) &#123;
&nbsp;&nbsp;die($objProxy -&gt; getError(PP_ERROR_MESSAGE));
&#125; 

?&gt;
</textarea><br/><br/>PHP Curl模拟提交，支持代理：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
define ( &#039;IS_PROXY&#039;, true ); //是否启用代理
/* cookie文件 */
$cookie_file = dirname ( __FILE__ ) . &quot;/cookie_&quot; . md5 ( basename ( __FILE__ ) ) . &quot;.txt&quot;; // 设置Cookie文件保存路径及文件名
/*模拟浏览器*/
$user_agent = &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)&quot;;

function vlogin($url, $data) &#123; // 模拟登录获取Cookie函数
&nbsp;&nbsp;$curl = curl_init (); // 启动一个CURL会话
&nbsp;&nbsp;if (IS_PROXY) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;//以下代码设置代理服务器
&nbsp;&nbsp;&nbsp;&nbsp;//代理服务器地址
&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS [&#039;proxy&#039;] );
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS [&#039;user_agent&#039;] ); // 模拟用户使用的浏览器
&nbsp;&nbsp;@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_COOKIEJAR, $GLOBALS [&#039;cookie_file&#039;] ); // 存放Cookie信息的文件名称
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS [&#039;cookie_file&#039;] ); // 读取上面所储存的Cookie信息
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 设置超时限制防止死循环
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
&nbsp;&nbsp;$tmpInfo = curl_exec ( $curl ); // 执行操作
&nbsp;&nbsp;if (curl_errno ( $curl )) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;Errno&#039; . curl_error ( $curl );
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;curl_close ( $curl ); // 关闭CURL会话
&nbsp;&nbsp;return $tmpInfo; // 返回数据
&#125;

function vget($url) &#123; // 模拟获取内容函数
&nbsp;&nbsp;$curl = curl_init (); // 启动一个CURL会话
&nbsp;&nbsp;if (IS_PROXY) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;//以下代码设置代理服务器
&nbsp;&nbsp;&nbsp;&nbsp;//代理服务器地址
&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS [&#039;proxy&#039;] );
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS [&#039;user_agent&#039;] ); // 模拟用户使用的浏览器
&nbsp;&nbsp;@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 发送一个常规的Post请求
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS [&#039;cookie_file&#039;] ); // 读取上面所储存的Cookie信息
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
&nbsp;&nbsp;$tmpInfo = curl_exec ( $curl ); // 执行操作
&nbsp;&nbsp;if (curl_errno ( $curl )) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;Errno&#039; . curl_error ( $curl );
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;curl_close ( $curl ); // 关闭CURL会话
&nbsp;&nbsp;return $tmpInfo; // 返回数据
&#125;

function vpost($url, $data) &#123; // 模拟提交数据函数
&nbsp;&nbsp;$curl = curl_init (); // 启动一个CURL会话
&nbsp;&nbsp;if (IS_PROXY) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;//以下代码设置代理服务器
&nbsp;&nbsp;&nbsp;&nbsp;//代理服务器地址
&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS [&#039;proxy&#039;] );
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 对认证证书来源的检查
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 从证书中检查SSL加密算法是否存在
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS [&#039;user_agent&#039;] ); // 模拟用户使用的浏览器
&nbsp;&nbsp;@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自动跳转
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_POST, 1 ); // 发送一个常规的Post请求
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的数据包
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS [&#039;cookie_file&#039;] ); // 读取上面所储存的Cookie信息
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 设置超时限制防止死循环
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 显示返回的Header区域内容
&nbsp;&nbsp;curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 获取的信息以文件流的形式返回
&nbsp;&nbsp;$tmpInfo = curl_exec ( $curl ); // 执行操作
&nbsp;&nbsp;if (curl_errno ( $curl )) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &#039;Errno&#039; . curl_error ( $curl );
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;curl_close ( $curl ); // 关键CURL会话
&nbsp;&nbsp;return $tmpInfo; // 返回数据
&#125;

function delcookie($cookie_file) &#123; // 删除Cookie函数
&nbsp;&nbsp;unlink ( $cookie_file ); // 执行删除
&#125;
?&gt;
</textarea>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [PHP代理]用php实现proxy功能的示范及 PHP CURL 模拟提交（支持代理）]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>