简体   繁体   English

fsockopen有错误:HTTP / 1.1 301永久移动和404

[英]fsockopen have errors : HTTP/1.1 301 Moved Permanently and 404

I have used this code to open whatismyipaddress.com 我已使用此代码打开whatismyipaddress.com

$fp = fsockopen("whatismyipaddress.com", 80, $errno, $errstr, 5);

if ($fp) {
    $url = "/";

    fputs($fp, "GET $url HTTP/1.1\r\nHost: {whatismyipaddress.com}\r\nConnection: close\r\n\r\n");
    $resp = '';

    while(!feof($fp)) {
        $resp .= fgets($fp, 1024);
    }

    echo "$resp";
}

and i always can see this error 我总能看到这个错误

HTTP/1.1 301 Moved Permanently Date: Tue, 29 Nov 2011 20:19:36 GMT Server: Apache/2.2.17 (Unix) DAV/2 Location: http://whatismyipaddress.com/ MS-Author-Via: DAV Content-Length: 0 Connection: close Content-Type: text/html HTTP / 1.1 301永久移动日期:2011年11月29日,星期二20:19:36 GMT服务器:Apache / 2.2.17(Unix)DAV / 2位置: http ://whatismyipaddress.com/ MS-Author-Via:DAV内容-Length:0连接:close内容类型:text / html

Also i have used this code to open whatismyipaddress.com/proxy-check 我也使用此代码打开whatismyipaddress.com/proxy-check

$fp = fsockopen("whatismyipaddress.com", 80, $errno, $errstr, 5);

if ($fp) {
    $url = "/proxy-check";

    fputs($fp, "GET $url HTTP/1.1\r\nHost: {whatismyipaddress.com}\r\nConnection: close\r\n\r\n");
    $resp = '';

    while(!feof($fp)) {
        $resp .= fgets($fp, 1024);
    }

    echo "$resp";
}

and have this error 并有这个错误

HTTP/1.1 404 Not Found Date: Tue, 29 Nov 2011 20:32:07 GMT Server: Apache/2.2.17 (Unix) DAV/2 Content-Length: 421 Connection: close Content-Type: text/html; 找不到HTTP / 1.1 404日期:2011年11月29日,星期二20:32:07 GMT服务器:Apache / 2.2.17(Unix)DAV / 2内容长度:421连接:close内容类型:text / html; charset=iso-8859-1 Not Found charset = iso-8859-1找不到

The requested URL /proxy-check was not found on this server. 在此服务器上找不到请求的URL / proxy-check。

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 此外,尝试使用ErrorDocument处理请求时遇到404 Not Found错误。 Apache/2.2.17 (Unix) DAV/2 Server at {whatismyipaddress.com} Port 80 位于{whatismyipaddress.com}端口80的Apache / 2.2.17(Unix)DAV / 2服务器

I'm sure, there is no any problem with the codes. 我敢肯定,代码没有任何问题。 i have tested it with many sites and i didn't get any problem 我已经在许多网站上对其进行了测试,但没有遇到任何问题

Please can anyone explain this problem ? 请问有人可以解释这个问题吗?

Thank you. 谢谢。

I would try to use another user-agent in headers. 我会尝试在标头中使用另一个用户代理。 I'm sure they are blocking robots using some sort of header based protection. 我敢肯定他们使用某种基于头的保护来阻止机器人。

Using curl: 使用curl:

$ curl -I 'http://whatismyipaddress.com'
HTTP/1.1 403 Forbidden
Date: Tue, 29 Nov 2011 20:48:28 GMT
Server: Apache/2.2.17 (Unix) DAV/2
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=iso-8859-1

However, once trying a forced user agent it works: 但是,一旦尝试强制用户代理,它就可以工作:

$ curl -I -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0.1) Gecko/20100101 Firefox/8.0.1' 'http://whatismyipaddress.com'
HTTP/1.1 200 OK
Date: Tue, 29 Nov 2011 20:49:24 GMT
Server: Apache/2.2.17 (Unix) DAV/2
Set-Cookie: pt=f737a9bb1a119dcec75073f11b05d213; expires=Wed, 30-Nov-2011 20:49:24 GMT
MS-Author-Via: DAV
Vary: Accept-Encoding
Content-Type: text/html

Basically your script works fine. 基本上你的脚本工作正常。 There are some errors. 有一些错误。 One is specific to HTTP, see this line of code: 一个特定于HTTP,请参阅以下代码行:

fputs($fp, "GET $url HTTP/1.1\r\nHost: {whatismyipaddress.com}\r\nCon ...
                                       ^                     ^

Remove those brackets, the HTTP protocol does not have any of those there, you need to provide a valid hostname. 删除那些括号,HTTP协议中没有任何那些,您需要提供有效的主机名。 Solution: 解:

fputs($fp, "GET $url HTTP/1.1\r\nHost: whatismyipaddress.com\r\nCon ...

Then the remote site will tell you need a user agent. 然后远程站点将告诉您需要用户代理。 Add it as an additional header: 将其添加为其他标题:

fputs($fp, "GET $url HTTP/1.1\r
Host: whatismyipaddress.com\r
Connection: close\r
User-Agent: Florian der Fensterputzer\r\n\r\n");

This will do it. 这样做。 Demo . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM