4

我有一个 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;  
    } 
4

1 回答 1

9

没有代理:

<?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

于 2011-09-02T15:39:11.587 回答