note 94831 modified in function.fopen by danbrown
| From: | danbrown@php.net | Date: | Fri, 27 Nov 2009 13:47:36 +0000 |
| Subject: | note 94831 modified in function.fopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163333@lists.php.net to get a copy of this message | ||
Hello all. I have had trouble getting files through proxy, and the solution posted above only works
if the requested file is also hosted on the proxy server (which is quite unlikely). so I've put
together the following functions pfopen & preadfile. they quite work like the replaced fopen
& readfile, and will resort to them if no proxy is provided :)
<?php
function preadfile($_url, $_proxy_name = null, $_proxy_port = 4480){
if(is_null($_proxy_name) || LOCAL_TEST){
return readfile($_url);
}else{
$proxy_cont = '';
$proxy_fp = pfopen($_url, $_proxy_name, $_proxy_port);
while(!feof($proxy_fp)) {$proxy_cont .= fread($proxy_fp,4096);}
fclose($proxy_fp);
$proxy_cont = substr($proxy_cont, strpos($proxy_cont,"\r\n\r\n")+4);
echo $proxy_cont;
return count($proxy_cont);
}
}
function pfopen($_url, $_proxy_name = null, $_proxy_port = 4480) {
if(is_null($_proxy_name) || LOCAL_TEST){
return fopen($_url);
}else{
$proxy_fp = fsockopen($_proxy_name, $_proxy_port);
if (!$proxy_fp) return false;
$host= substr($_url, 7);
$host = substr($bucket, 0, strpos($host, "/"));
$request = "GET $_url HTTP/1.0\r\nHost:$host\r\n\r\n";
fputs($proxy_fp, $request);
return $proxy_fp;
}
}
?>
--was--
Hello all. I have had trouble getting files through proxy, and the solution posted above only works
if the requested file is also hosted on the proxy server (which is quite unlikely). so I've put
together the following functions pfopen & preadfile. they quite work like the replaced fopen
& readfile, and will resort to them if no proxy is provided :)
function preadfile($_url, $_proxy_name = null, $_proxy_port = 4480){
if(is_null($_proxy_name) || LOCAL_TEST){
return readfile($_url);
}else{
$proxy_cont = '';
$proxy_fp = pfopen($_url, $_proxy_name, $_proxy_port);
while(!feof($proxy_fp)) {$proxy_cont .= fread($proxy_fp,4096);}
fclose($proxy_fp);
$proxy_cont = substr($proxy_cont, strpos($proxy_cont,"\r\n\r\n")+4);
echo $proxy_cont;
return count($proxy_cont);
}
}
function pfopen($_url, $_proxy_name = null, $_proxy_port = 4480) {
if(is_null($_proxy_name) || LOCAL_TEST){
return fopen($_url);
}else{
$proxy_fp = fsockopen($_proxy_name, $_proxy_port);
if (!$proxy_fp) return false;
$host= substr($_url, 7);
$host = substr($bucket, 0, strpos($host, "/"));
$request = "GET $_url HTTP/1.0\r\nHost:$host\r\n\r\n";
fputs($proxy_fp, $request);
return $proxy_fp;
}
}
http://php.net/manual/en/function.fopen.php