这个应该用socket连接,发送http请求,《php高级编程》上有例子。
但是我运行下面的代码也有问题,虽然文件句柄是有了,可最后地址成了
http://localhost/temp/http/phpfiles/zxks/main/load.php,其中temp/http/是该脚本在我机器上的目录。不知道是怎么回事。-----httpclass.php----------
<?phpclass http {
   var $proxy_host = "";
   var $proxy_port = 0;   function http_fopen($host, $path, $port = 80) {      # has the user set $proxy_host?
      if(empty($this->proxy_host)) {
         # we access the server directly
         $conn_host = $host;
         $conn_port = $port;
      } else {
         # we use the proxy
         $conn_host = $this->proxy_host;
         $conn_port = $this->proxy_port;
      }        # build the absolute URL
      $abs_url = "http://$host:$port$path";      # now we build our query
      $query = "GET $abs_url HTTP/1.0\r\n".
               "Host: $host:$port\r\n".
               "User-agent: PHP/class http 0.1\r\n".
               "\r\n";      # open a connection to the server
      $fp = fsockopen($conn_host, $conn_port);      # if the connection failed, return false
      if(!$fp) 
         return false;      # send our query
      fputs($fp, $query);      # discard the HTTP header
      while(trim(fgets($fp, 1024)) != "");      # return the active file pointer
      return $fp;
   }
}
?>
--------------http.php-----------------------
<?php   include "httpclass.php";   $http = new http;   $fp = $http->http_fopen("qhaha.51.net", "/zxks.htm");;   if(!$fp) {
      print "Sorry, the server is not currently available";
      exit;
   }  print '<BASE HREF=\"http://qhaha.51.net/\"><p>';  fpassthru($fp);
?>

解决方案 »

  1.   

    <?
    $url='http://qhaha.51.net/zxks.htm';
    $fcontents = file ($url);
    while (list ($line_num, $line) = each ($fcontents)) {
        echo "<b>Line $line_num:</b>; ", htmlspecialchars ($line), "<br>\n";
    }
    ?>
    成功,顯示結果如下:
    Line 0:; <html> 
    Line 1:; 
    Line 2:; <head> 
    Line 3:; <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    Line 4:; <meta name="GENERATOR" content="Microsoft FrontPage 5.0"> 
    Line 5:; <meta name="ProgId" content="FrontPage.Editor.Document"> 
    Line 6:; <title>起始页</title> 
    Line 7:; 
    Line 8:; <SCRIPT LANGUAGE="JScript"> 
    Line 9:; 
    Line 10:; window.location.href='phpfiles/zxks/main/load.php'; 
    Line 11:; 
    Line 12:; </SCRIPT> 
    Line 13:; 
    Line 14:; </head> 
    Line 15:; 
    Line 16:; <body> 
    Line 17:; 
    Line 18:; </body> 
    Line 19:; 
    Line 20:; </html>
      

  2.   

    或者可以使用fopen打开远程文件,然后用fread来读取.
      

  3.   

    借wasy的程序改来用用:
    <?
    $url='http://qhaha.51.net/zxks.htm';
    $fp=fopen($url,"r");
    $fcontents = fread($fp,10000000);
    fclose($fp);
    echo $fcontents;
    ?>
      

  4.   

    leeMaRS:
    fopen好像函數行不通,参数以http://开头,则会打开这个url,这一点好像同readfile一样,如下。楼主第一種方法应该行不通。
    readfile只是返回文件的长度,如果以http://开头,则会打开这个url。
    如下说的是readfile的一个用法:
    If filename begins with "http://" (not case sensitive), an HTTP 1.0 connection is opened to the specified server and the text of the response is written to standard output. 我也一直将file记成了readfile,哈哈
      

  5.   

    此程序无效:<?
    $url='http://qhaha.51.net/zxks.htm';
    $fcontents = file ($url);
    while (list ($line_num, $line) = each ($fcontents)) {
        echo "<b>Line $line_num:</b>; ", htmlspecialchars ($line), "<br>\n";
    }
    ?>Warning: php_hostconnect: connect failed in /z2/qhaha/public_html/ttt.php on line 3Warning: file("http://qhaha.51.net/zxks.htm") - Bad file descriptor in /z2/qhaha/public_html/ttt.php on line 3Warning: Variable passed to each() is not an array or object in /z2/qhaha/public_html/ttt.php on line 4我认为肯定是屏蔽了什么东西了,但我不明白,为什么FOPEN函数能打开本地文件,却无法打开URL,是不是把某些功能从这些函数里屏蔽了?
      

  6.   

    对于darzui(牛肉饭)朋友的程序,出错代码为:Sorry, the server is not currently available
      

  7.   

    看来使用PHP方法是无法获取远程文件了,我去JSCRIPT组看看有没有方法,谢谢大家!!我已经给51。NET的技术支持发了EMAIL,看看他们怎么说吧
      

  8.   

    QQKiKi(KiKi) :
    我把结果都显示在上面了,你还不相信php?
    可能错误在其他地方.
    hehe
      

  9.   

    绝绝对对不可能说会有"PHP无法获取远程文件"这种说法,偶的老虎头可以做保证.
    to wasy : 我记得用fopen是可以的.因为我以前是这么用的.打开这个URL?呵呵,我觉得你是不是认为HTML的解析工具是在服务器完成了?...应该是浏览器完成的,浏览器拿到HTML后自己解析的:)
      

  10.   

    Warning: php_hostconnect: connect failed in /z2/qhaha/public_html/ttt.php on line 3错误 : 连续远程服务器失败.我开始在想会不会是你想去读的URL不能访问.
      

  11.   

    <?
    $query = "GET http://qhaha.51.net:80 HTTP/1.0\r\n"."Host: qhaha.51.net:$port\r\n"."User-agent: PHP/class http 0.1\r\n"."\r\n";
    $fp = fsockopen("qhaha.51.net", "80");
    if (!$fp)
    {
    echo "无法连接";
    }
    fputs($fp, $query);
    fpassthru($fp);
    ?>
    可以连到你的网站,并且输出首页,但是
    <?
    $query = "GET http://qhaha.51.net:80/zxks.htm HTTP/1.0\r\n"."Host: qhaha.51.net:$port\r\n"."User-agent: PHP/class http 0.1\r\n"."\r\n";
    $fp = fsockopen("qhaha.51.net", "80");
    if (!$fp)
    {
    echo "无法连接";
    }
    fputs($fp, $query);
    fpassthru($fp);
    ?>
    就不行了,不知道是怎么回事。
      

  12.   

    你给的路径是自动转到http://qhaha.51.net/phpfiles/user/login1.php的,所以用下面的代码可以连接,我在自己的机器上测试成功.
    <?
    $query = "GET http://qhaha.51.net:80/phpfiles/user/login1.php HTTP/1.0\r\n"."Host: qhaha.51.net:$port\r\n"."User-agent: PHP/class http 0.1\r\n"."\r\n";
    $fp = fsockopen("qhaha.51.net", "80");
    if (!$fp)
    {
    echo "无法连接";
    }
    fputs($fp, $query);
    fpassthru($fp);
    ?>
      

  13.   

    好啦好啦,谢谢大家,不要讨论了,是我的主机51。NET屏蔽了FOPEN的外部连接功能,所以我上传上去就没法用了。我做了ACTIVEX控件了,好使~~ :)
      

  14.   

    你申请的空间主机php.ini有限制,不能执行打开远程文件操作!!!