【问题标题】:Trying to get the HTTP code of a page using PHP尝试使用 PHP 获取页面的 HTTP 代码
【发布时间】:2018-04-08 11:39:05
【问题描述】:

我正在尝试检查页面是否已关闭、404、500、403 等。

我正在使用这个

function is_404($url) {
    $handle = curl_init($url);
    curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

    /* Get the HTML or whatever is linked in $url. */
    $response = curl_exec($handle);

    /* Check for 404 (file not found). */
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    curl_close($handle);

    /* If the document has loaded successfully without any redirection or error */
    if ($httpCode >= 200 && $httpCode < 302) {
        return $httpCode;
    } else {
        return $httpCode;
    }
}

但是,我得到的只是来自所有网站的 301:/ return $httpCode 是为了检查我得到了什么。

我正在尝试这个

function getHttpResponseCode_using_curl($url, $followredirects = true){
        // returns int responsecode, or false (if url does not exist or connection timeout occurs)
        // NOTE: could potentially take up to 0-30 seconds , blocking further code execution (more or less depending on connection, target site, and local timeout settings))
        // if $followredirects == false: return the FIRST known httpcode (ignore redirects)
        // if $followredirects == true : return the LAST  known httpcode (when redirected)
        if(! $url || ! is_string($url)){
            return false;
        }
        $ch = @curl_init($url);
        if($ch === false){
            return false;
        }
        @curl_setopt($ch, CURLOPT_HEADER         ,true);    // we want headers
        @curl_setopt($ch, CURLOPT_NOBODY         ,true);    // dont need body
        @curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);    // catch output (do NOT print!)
        if($followredirects){
            @curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,true);
            @curl_setopt($ch, CURLOPT_MAXREDIRS      ,10);  // fairly random number, but could prevent unwanted endless redirects with followlocation=true
        }else{
            @curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,false);
        }
//      @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);   // fairly random number (seconds)... but could prevent waiting forever to get a result
//      @curl_setopt($ch, CURLOPT_TIMEOUT        ,6);   // fairly random number (seconds)... but could prevent waiting forever to get a result
//      @curl_setopt($ch, CURLOPT_USERAGENT      ,"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");   // pretend we're a regular browser
        @curl_exec($ch);
        if(@curl_errno($ch)){   // should be 0
            @curl_close($ch);
            return false;
        }
        $code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); // note: php.net documentation shows this returns a string, but really it returns an int
        @curl_close($ch);
        return $code;
    }

我在https://face.gta.world/sign-in/ 上进行了尝试,您可以在视觉上看到 500 错误,上面的代码返回 200 错误

【问题讨论】:

  • if ($httpCode &gt;= 200 &amp;&amp; $httpCode &lt; 302) 你说你得到的只是301;这就是它的作用。您可能想改用OR 运算符。但是,这可能取决于您要检查的 url,我们不知道它们是什么。
  • 在上面的答案中添加更多代码
  • “您可以直观地看到 500 错误” - 然后检查您的日志。
  • 通过伪造真实浏览器修复@curl_setopt($ch, CURLOPT_USERAGENT ,"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");

标签: php


【解决方案1】:

试试这个

$url= 'http://google.com';

function get_http_response_code($url) {
  $headers = get_headers($url);
  return substr($headers[0], 9, 3);
 }

$get_http_response_code = get_http_response_code($url);

echo $get_http_response_code;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2019-12-15
    • 2012-08-01
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多