这个应该用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);
?>
但是我运行下面的代码也有问题,虽然文件句柄是有了,可最后地址成了
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);
?>
$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>
<?
$url='http://qhaha.51.net/zxks.htm';
$fp=fopen($url,"r");
$fcontents = fread($fp,10000000);
fclose($fp);
echo $fcontents;
?>
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,哈哈
$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,是不是把某些功能从这些函数里屏蔽了?
我把结果都显示在上面了,你还不相信php?
可能错误在其他地方.
hehe
to wasy : 我记得用fopen是可以的.因为我以前是这么用的.打开这个URL?呵呵,我觉得你是不是认为HTML的解析工具是在服务器完成了?...应该是浏览器完成的,浏览器拿到HTML后自己解析的:)
$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);
?>
就不行了,不知道是怎么回事。
<?
$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);
?>