I need to go through a proxy, (from a running fsockopen code that gets the size from a remote video file)

Fabrefumi picture Fabrefumi · Sep 2, 2011 · Viewed 7.7k times · Source

I've a PHP running code that ask the file size of a remote mp4 file, thanks to a fsockopen function and HEAD command.

Now, i need to move this code to other server behind a proxy, which is the best approach to go through that new proxy, and continue using fsockopen? I'm really stuck. I can't tunnel or handle two sockets.

Any ideas? thanks for your help and time.

private function filesize_remote($remotefile, $timeout=10) {
       $size = false;
       $url = parse_url($remotefile);

       if ($fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno, $errstr, $timeout)) {
          fwrite($fp, 'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n".'Host: '.@$url['host']."\r\n\r\n");
          while (!feof($fp)) {
             $headerline = fgets($fp, 4096);
             if (preg_match('/^Content-Length: (.*)/', $headerline, $matches)) {
                $size = intval($matches[1]);
                break;
             }
          }
          fclose ($fp);
       }

       return $size;  
    } 

Answer

glarkou picture glarkou · Sep 2, 2011

Without proxy:

<?php
$fp = fsockopen("www.wahoo.com",80);

fputs($fp, "GET <a href=\"http://www.yahoo.com/\" "
  ."title=\"http://www.yahoo.com/\">http://www.yahoo.com/</a> HTTP/1.0\r\n\r\n");

$data="";
while (!feof($fp)) $data.=fgets($fp,64000);
fclose($fp);

print $data;
?>

With proxy:

<?php
$ip = "1.2.3.4"; // proxy IP, change this according to your proxy setting
$port = 1234; // proxy port, change this according to your proxy setting

$fp = fsockopen($ip,$port); // connect to proxy
fputs($fp, "GET <a href=\"http://www.yahoo.com/\"   "
  . "title=\"http://www.yahoo.com/\">http://www.yahoo.com/</a>  "
  . "HTTP/1.0\r\nHost:www.yahoo.com:80\r\n\r\n");

$data="";
while (!feof($fp)) $data.=fgets($fp,64000);
fclose($fp);

print $data;
?>

With proxy and authentication:

<?php
$ip = "1.2.3.4"; // proxy IP, change this according to your proxy setting
$port = 1234; // proxy port, change this according to your proxy setting

$fp = fsockopen($ip,$port); // connect to proxy

$login = "Alexander"; // login name
$passwd = "kiss me"; // password

fputs($fp, "GET <a href=\"http://www.yahoo.com/\" "
 . "title=\"http://www.yahoo.com/\">http://www.yahoo.com/</a> HTTP/1.1\r\n"
 . "Host:www.yahoo.com:80\r\n"
 . "Proxy-Authorization: Basic ".base64_encode("$login:$passwd") ."\r\n\r\n");

$data="";
while (!feof($fp)) $data.=fgets($fp,64000);
fclose($fp);

//12314
print $data;
?>

Look here: Fsockopen with proxy