【问题标题】:cURL weird status codes when checking URL检查 URL 时出现 cURL 奇怪的状态代码
【发布时间】:2013-12-12 00:39:41
【问题描述】:

我正在检查不同 URL 上是否存在 xml 站点地图。如果我提供一个 URL example.com/sitemap.xml,并且它对 www.example.com/sitemap.xml 有一个 301,我显然会得到一个 301。如果 www.example.com/sitemap.xml 不存在,我不会看到 404。因此,如果我得到 301,我会执行另一个 cURL 来查看 www.example.com/sitemap.xml 是否返回 404。但是,出于某种原因,我得到了随机的 404 和 303 状态码。

private function check_http_status($domain,$file){

        $url = $domain . "/" . $file;

        $curl = new Curl();

        $curl->url = $url;
        $curl->nobody = true;
        $curl->userAgent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20060601 Firefox/2.0.0.1 (Ubuntu-edgy)';
        $curl->execute();
        $retcode = $curl->httpCode();

        if ($retcode == 301 || $retcode == 302){

            $url = "www." . $domain . "/" . $file;

            $curl = new Curl();
            $curl->url = $url;
            $curl->nobody = true;
            $curl->userAgent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20060601 Firefox/2.0.0.1 (Ubuntu-edgy)';
            $curl->execute();
            $retcode = $curl->httpCode();

        }

        return $retcode;

    }

【问题讨论】:

标签: php curl


【解决方案1】:

查看返回的响应代码列表 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

通常网络浏览器会自动处理这些,但是当您使用 curl 手动执行操作时,您需要了解每个响应的含义。 301302 表示您应该使用提供的替代 url 来访问资源。这可能很简单,例如将 www 添加到请求中,但也可能更复杂,因为重定向到不同的域。

303 表示您正在使用POST 尝试访问资源,应该使用GET

【讨论】:

  • 我可能已经弄清楚了。如果我只是跟随位置,它将返回重定向位置的状态代码。
【解决方案2】:

好吧,当您收到 301 或 302 时,您应该使用在响应中找到的位置,而不是假设另一个位置并尝试。

正如您在此示例中看到的,来自服务器的响应包含文件的新位置。将其用于您的下一个请求: http://en.wikipedia.org/wiki/HTTP_301#Example

【讨论】:

  • 您可以假设 sitemap.xml 的位置。如果它不存在,我想知道。
  • 实际上,我收回了这一点。我刚问了一个同事,他说sitemap.xml可以叫别的东西。我从来没有想过。我想,我会将可能的位置插入到一个数组中,但是对于每个位置,我都想获得一个真实的状态。我不知道 303 是什么。
  • 不要这样做(将可能的位置插入到数组中)。只需从响应中获取新位置。这样,您的尝试次数将更少,效率更高,您的脚本也将根据 http 协议运行。假设服务器返回重定向的真实位置,它应该返回。
【解决方案3】:

“followLocation”效果很好。以下是我的实现方式:

$url = "http://www.YOURSITE.com//"; // Assign you url here.

$ch = curl_init(); // initialize curl.
curl_setopt($ch, CURLOPT_URL, $url); // Pass the URL as the option/target.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 0 will print html. 1 does not.
curl_setopt($ch, CURLOPT_HEADER, 0); // Please curl, inlude the header in the output.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // ..and yes, follow what the server sends as part of the HTTP header.

$response_data = curl_exec($ch); // execute curl with the target URL.
$http_header = curl_getinfo($ch); // Gets information about the last transfer i.e. our URL
// Print the URLs that are not returning 200 Found.
if($http_header['http_code'] != "200") {
    echo " <b> PAGE NOT FOUND => </b>"; print $http_header['http_code'];
}
// print $http_header['url']; // Print the URL sent back in the header. This will print the page to wich you were redirected.
print $url; // this will print the original URLs that you are trying to access

curl_close($ch); // we are done with curl; so let's close it.

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多