我有一个PHP运行代码,询问远程mp4文件的文件大小,这要归功于fsockopen函数和HEAD命令。
现在,我需要将此代码移动到代理后面的其他服务器,这是通过新代理的最佳方法,并继续使用fsockopen?我真的卡住了。我不能隧道或处理两个套接字。
任何想法?谢谢你的帮助和时间。
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;
}
无代理:
<?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;
?>
与代理:<?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;
?>
使用代理和认证:
<?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;
?>
看这里:Fsockopen with proxy