Table des matières
点击此处通过 PhpProxy 下载文件" >点击此处通过 PhpProxy 下载文件
Maison php教程 php手册 用php实现proxy功能的示范 [已升级至v0.0.2]

用php实现proxy功能的示范 [已升级至v0.0.2]

Jun 21, 2016 am 09:15 AM
function gt quot return this

刚刚写的练手的作品。感觉还是有些实用价值的。。放出来给大家试试。
功能:通过php实现代理功能
适用用户:在家下载较慢。且懒得找proxy。且有高速主机,且主机有php,且主机支持socket。
使用方法:
1。把本程序传到主机上
2。通过浏览器浏览主机上的此文件
3。在url中填入欲下载的文件地址(如果有必要,可以填入referer)
4。点击get proxy url
5。新打开的页面中的链接即为通过proxy下载的链接

注意:
1。支持多线程下载,断点续传
2。仅支持http协议
3。不支持服务器端header重定向
4。不支持http auth
5。下载较大的文件时,可以修改程序中set_time_limit的值,以减少断线的现象(默认为180,即3分钟)


PHP代码:--------------------------------------------------------------------------------

/***************************************/
/* */
/* Php Http Proxy */
/* version: 0.0.2 */
/* last modify: 2005.1.12 */
/* author: q3boy */
/* */
/***************************************/
set_time_limit(180);

define('PP_ERROR_CODE',1);
define('PP_ERROR_MESSAGE',2);
define('PP_ERROR_ALL',3);
define('PP_RETURN_STRING',1);
define('PP_RETURN_ARRAY',2);
define('PP_RETURN_ALL',3);

class CPhpProxy {

var $_strUrl;
var $_arrArgv;
var $_strRef;
var $_arrUrl;
var $_resSocket;
var $_intError;
var $_strError;
var $_arrDefaultArrayUrl;
var $_arrDefaultPort;

function CPhpProxy($strUrl = null, $arrArgv = array()) {
$this->init();
is_null($strUrl)?'':$this->setUrl($strUrl);
is_null($arrArgv['refer'])?'':$this->setRef($arrArgv['refer']);
$this->proxy($strUrl,$arrArgv);
}
function setError($intError,$mixArgv=null) {
$this->_intError = $intError;
if($intError==1001) {
$this->_strError = "调用 $mixArgv 方法时参数传递出错, 枚举值不存在";
}elseif($intError==1) {
$this->_strError = "url 错误, url 格式不正确.\n".$mixArgvs;
}elseif($intError == 2) {
$this->_strError = "url 错误, 目前尚未支持此协议.\n".$mixArgvs;
}elseif($intError == 3) {
$this->_strError = "无法打开 ".$this->_arrUrl['host'].':'.$this->_arrUrl['port']."\n".$mixArgv[0].': '.$mixArgv[1];
}

Return false;
}

function getError($enumReturnType=PP_ERROR_CODE) {
if($enumReturnType==PP_ERROR_ALL) {
Return array(str_pad($this->_intError,5,'0',STR_PAD_LEFT),$this->_strError);
}elseif($enumReturnType==PP_ERROR_MESSAGE) {
Return $this->_strError;
}elseif($enumReturnType==PP_ERROR_CODE) {
Return $this->_intError;
}else {
Return false;
}
}

function init() {
$this->_strUrl = null;
$this->_arrArgv = array();
$this->_strRef = null;
$this->_arrUrl = array();
$this->_resSocket = null;
$this->_intError = 0;
$this->_strError = 'there is no error';
//arrurl 默认值
$this->_arrDefaultArrayUrl = array('protocol'=>'http','file'=>'index.htm');
//默认端口
$this->_arrDefaultPort =array('http'=>'80');
Return true;
}
function setUrl($strUrl) {
$strUrl = trim($strUrl);
//正则
$reg = "/^(([\w]+):\/\/)?([^:|\/|@]*:)?([^:|\/|@]*@)?([\w|\-|\.]+)(:\d+)?(\/[^\?|#]*)?(\?[^#]*)?(#.*)?$/is";
if(!preg_match($reg,$strUrl,$arrUrl)) {
Return $this->setError(1,$strUrl);
}else {
//拆解匹配数组
list($tmp, $tmp, $arr['protocol'], $arr['user'], $arr['pass'], $arr['host'], $arr['port'], $arr['path'], $arr['query'], $arr['anchor']) = $arrUrl;
//默认协议
if(!$arr['protocol']) {
$arrUrl[1] = $this->_arrDefaultArrayUrl['protocol'].'://';
}
//设 filename
$arr['file'] = basename($arr['path']);
//默认值
foreach($this->_arrDefaultArrayUrl as $key=>$val) {
if($arr[$key] == '') {
$arr[$key] = $val;
}
}
//默认端口
if(is_null($this->_arrDefaultPort[$arr['protocol']])) {
Return $this->setError(2,$arr['protocol']);
}elseif(!$arr['port']) {
$arr['port'] = $this->_arrDefaultPort[$arr['protocol']];
}

//设 uri
$arr['uri'] = ($arr['path']?$arr['path']:'/') . ($arr['query']?'?'.$arr['query']:'') . ($arr['anchor']?'#'.$arr['anchor']:'');
//设 url
unset($arrUrl[0]);
unset($arrUrl[2]);
$this->_strUrl = implode('',$arrUrl);
//设 arrurl
$this->_arrUrl = $arr;
Return true;
}
}
function getUrl($enumReturnType = PP_RETURN_ARRAY) {
if($enumReturnType==PP_RETURN_STRING) {
Return $this->_strUrl;
}elseif($enumReturnType==PP_RETURN_ARRAY) {
Return $this->_arrUrl;
}if($enumReturnType==PP_RETURN_ALL) {
Return array($this->_strUrl,$this->_arrUrl);
}else {
Return $this->setError(1001);
}

}
function setRefer($strRef) {
$this->_strRef = trim($strRef);
Return true;
}
function getRefer() {
Return $this->_strRef;
}

function getProxyUrl() {
global $_SERVER;
list($strProcotol) = explode('/',strtolower(trim($_SERVER['SERVER_PROTOCOL'])));
$str = $strProcotol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].
'?u='.urlencode($this->getUrl(PP_RETURN_STRING)).
($this->getRefer()?('&r='.urlencode($this->getRefer())):'');
Return $str;
}
function openSocket() {
$arr = $this->getUrl();
$this->_resSocket = @fsockopen($arr['host'],$arr['port'],$intError, $strError, 30);
if(!$this->_resSocket) {
$this->_resSocket = null;
Return $this->setError(3,array($intError, $strError));
}else {
Return true;
}
}
function getRequest() {
Return $this->{'get'.ucfirst($this->_arrUrl['protocol']).'Request'}();
}
function sendRequest() {
Return fwrite($this->_resSocket,$this->getRequest());
}
function flushResponse() {
Return $this->{'flush'.ucfirst($this->_arrUrl['protocol']).'Response'}();
}
function getHttpRequest() {
$arr = $this->getUrl();
$arrRequest = getallheaders();
$arrRequest['Host'] = $arr['host'];
$strRequest = "GET ".$arr['uri']." HTTP/1.1\r\n";
foreach($arrRequest as $key=>$val) {
$strRequest .="$key: $val\r\n";
}
$strRequest .="\r\n";
Return $strRequest;
}
function flushHttpResponse() {
$bolHeader=true;
while (!feof($this->_resSocket)) {
$str = fgets($this->_resSocket, 4096);
if($bolHeader) {
@header($str);
}else {
echo($str);
}
if($bolHeader && $str=="\r\n") {
$bolHeader=false;
@header("Content-type: application/gzip");
@header('Content-Disposition: attachment; filename="'.$this->_arrUrl['file'].'"');
}
}
Return true;
}
function close() {
fclose($this->_resSocket);
$this->_resSocket = null;
Return true;
}
function proxy($strUrl = null,$arrArgv = array()) {
if(!is_null($this->getUrl(PP_RETURN_STRING)) && $this->openSocket() && $this->sendRequest() && $this->flushResponse()) {
echo(123);
$this->close();
}else {
Return false;
}
}
}
if(sizeof($_GET)) {
$strGetArrayName = '_GET';
}elseif(sizeof($HTTP_GET_VARS)) {
$strGetArrayName = 'HTTP_GET_VARS';
}else {
die('



PhpProxy



















PhpProxy
URL:
REFERER:





');
}
$strUrl = trim(${$strGetArrayName}['u']);
if($strUrl=='') {
die('请输入 url 地址.');
}
//get referer
$strRefTmp = trim(${$strGetArrayName}['r']);
//初始化 proxy 类
$objProxy = new CPhpProxy();
//设置 url 和 refer
$objProxy->setUrl($strUrl);
$objProxy->setRefer($strRef);
//错误输出
if($objProxy->getError()) {
die($objProxy->getError(PP_ERROR_MESSAGE));
}

//echo url for download
if(${$strGetArrayName}['act']=='make') {
die("

点击此处通过 PhpProxy 下载文件

");
}
//取文件
$objProxy->proxy();
//错误处理
if($objProxy->getError()) {
die($objProxy->getError(PP_ERROR_MESSAGE));
}
?>




Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Quelles sont les différences entre Huawei GT3 Pro et GT4 ? Quelles sont les différences entre Huawei GT3 Pro et GT4 ? Dec 29, 2023 pm 02:27 PM

Quelles sont les différences entre Huawei GT3 Pro et GT4 ?

Explication détaillée de l'utilisation de return en langage C Explication détaillée de l'utilisation de return en langage C Oct 07, 2023 am 10:58 AM

Explication détaillée de l'utilisation de return en langage C

Que signifie fonction ? Que signifie fonction ? Aug 04, 2023 am 10:33 AM

Que signifie fonction ?

Correctif : l'outil de capture ne fonctionne pas sous Windows 11 Correctif : l'outil de capture ne fonctionne pas sous Windows 11 Aug 24, 2023 am 09:48 AM

Correctif : l'outil de capture ne fonctionne pas sous Windows 11

Quel est l'ordre d'exécution des instructions return et enfin en Java ? Quel est l'ordre d'exécution des instructions return et enfin en Java ? Apr 25, 2023 pm 07:55 PM

Quel est l'ordre d'exécution des instructions return et enfin en Java ?

Comment réparer l'erreur Impossible de se connecter à l'App Store sur iPhone Comment réparer l'erreur Impossible de se connecter à l'App Store sur iPhone Jul 29, 2023 am 08:22 AM

Comment réparer l'erreur Impossible de se connecter à l'App Store sur iPhone

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

Explication détaillée du rôle et de la fonction de la table MySQL.proc Explication détaillée du rôle et de la fonction de la table MySQL.proc Mar 16, 2024 am 09:03 AM

Explication détaillée du rôle et de la fonction de la table MySQL.proc

See all articles