1
    $fileSource = "http://google.com";
    $ch = curl_init($fileSource);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($retcode != 200) {
        $error .= "The source specified is not a valid URL.";
    }
    curl_close($ch);

这是我的问题。当我使用上述并设置$fileSource = "http://google.com";它不起作用,而如果我将它设置为$fileSource = "http://www.google.com/";它工作。

有什么问题?

4

3 回答 3

1

One permanently redirects (301) to the www. domain, while the other one just replies OK (200).

Why are you only considering only the 200 status code as valid? Let CURL handle that for you:

curl_setopt($ch, CURLOPT_FAILONERROR, true);

From the manual:

TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The default behavior is to return the page normally, ignoring the code.

于 2012-05-04T04:07:44.137 回答
0

尝试明确告诉 curl 遵循重定向

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

如果这不起作用,您可能需要在某些站点上欺骗用户代理。

此外,如果他们使用 JS 重定向你运气不好。

于 2012-05-04T04:04:43.327 回答
0

您所看到的实际上是 301 重定向的结果。这是我从命令行使用详细 curl 得到的结果

curl -vvvvvv http://google.com
* About to connect() to google.com port 80 (#0)
*   Trying 173.194.43.34...
* connected
* Connected to google.com (173.194.43.34) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.25.0 (x86_64-apple-darwin11.3.0) libcurl/7.25.0 OpenSSL/1.0.1b zlib/1.2.6 libidn/1.22
> Host: google.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Fri, 04 May 2012 04:03:59 GMT
< Expires: Sun, 03 Jun 2012 04:03:59 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact
* Closing connection #0

但是,如果您在 301 重定向中建议的实际 www.google.com 上执行 curl,您将获得以下信息。

curl -vvvvvv http://www.google.com
* About to connect() to www.google.com port 80 (#0)
*   Trying 74.125.228.19...
* connected
* Connected to www.google.com (74.125.228.19) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.25.0 (x86_64-apple-darwin11.3.0) libcurl/7.25.0 OpenSSL/1.0.1b zlib/1.2.6 libidn/1.22
> Host: www.google.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 04 May 2012 04:05:25 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1

我截断了谷歌响应的其余部分,只是为了显示 200 OK 与 301 REDIRECT 的主要区别

于 2012-05-04T04:06:07.607 回答