note 94831 deleted from function.fopen by cmb
| From: | cmb@php.net | Date: | Fri, 09 Sep 2016 22:40:48 +0000 |
| Subject: | note 94831 deleted from function.fopen by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206556@lists.php.net to get a copy of this message | ||
Note Submitter: b dot evieux at gmail dot com
----
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;
}
}
?>