首页 文章

使用curl在PHP中获取HTTP代码

提问于
浏览
126

我正在使用CURL来获取站点的状态,如果它是向上/向下或重定向到另一个站点 . 我希望尽可能简化它,但它不能很好地运行 .

<?php
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return $httpcode;
?>

我把它包裹在一个函数中 . 它工作正常,但性能不是最好的,因为它下载整个页面,如果我删除 $output = curl_exec($ch); 的东西,它一直返回 0 .

有谁知道如何使性能更好?

7 回答

  • 2

    curl_exec 是必要的 . 尝试 CURLOPT_NOBODY 不下载正文 . 那可能会更快 .

  • 14
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $rt = curl_exec($ch);
    $info = curl_getinfo($ch);
    echo $info["http_code"];
    
  • 2

    curl_getinfo - 获取有关特定转移的信息

    检查curl_getinfo

    <?php
    // Create a curl handle
    $ch = curl_init('http://www.yahoo.com/');
    
    // Execute
    curl_exec($ch);
    
    // Check if any error occurred
    if(!curl_errno($ch))
    {
     $info = curl_getinfo($ch);
    
     echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
    }
    
    // Close handle
    curl_close($ch);
    
  • 85

    试试PHP的“get_headers”功能 .

    有点像:

    <?php
        $url = 'http://www.example.com';
        print_r(get_headers($url));
        print_r(get_headers($url, 1));
    ?>
    
  • 15
    // must set $url first....
    $http = curl_init($url);
    // do your curl thing here
    $result = curl_exec($http);
    $http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
    curl_close($http);
    echo $http_status;
    
  • 0

    这是我的解决方案需要获取Status Http来定期检查服务器的状态

    $url = 'http://www.example.com'; // Your server link
    
    while(true) {
    
        $strHeader = get_headers($url)[0];
    
        $statusCode = substr($strHeader, 9, 3 );
    
        if($statusCode != 200 ) {
            echo 'Server down.';
            // Send email 
        }
        else {
            echo 'oK';
        }
    
        sleep(30);
    }
    
  • 179

    首先确保URL实际上是否有效(字符串,非空,良好的语法),这可以快速检查服务器端 . 例如,首先执行此操作可以节省大量时间:

    if(!$url || !is_string($url) || ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url)){
        return false;
    }
    

    确保只获取 Headers ,而不是正文内容:

    @curl_setopt($ch, CURLOPT_HEADER  , true);  // we want headers
    @curl_setopt($ch, CURLOPT_NOBODY  , true);  // we don't need body
    

    有关获取URL状态http代码的更多详细信息,请参阅我发布的另一篇文章(它还有助于以下重定向):


    整体来说:

    $url = 'http://www.example.com';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);    // we want headers
    curl_setopt($ch, CURLOPT_NOBODY, true);    // we don't need body
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo 'HTTP code: ' . $httpcode;
    

相关问题