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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With