PHP:使用 Curl 验证 URL


PHP: Validating URL with Curl

    $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/";它就可以工作。

问题出在哪里?

一个永久重定向(301)到www.域,而另一个只回复确定(200)。

为什么只考虑 200 状态代码有效?让 CURL 为您处理:

curl_setopt($ch, CURLOPT_FAILONERROR, true);

从手册:

TRUE 表示,如果返回的 HTTP 代码大于 或,则以静默方式失败 等于 400。默认行为是正常返回页面, 忽略代码。

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

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

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

此外,如果他们使用JS会重定向您的运气。

您看到的实际上是 301 重定向的结果。这是我从命令行使用冗长的卷曲返回的内容

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 -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 重定向的主要区别