用于获取给定url的标头,正文和http代码的函数


Function for getting headers, body and http code for given url?

PHP函数获取标题,正文和http代码为给定的url ?Curl库没有最好的机制来解析和操作http头,所以这个函数将非常方便。对于需要这种功能的小脚本,使用一些大而健壮的库是令人厌烦的。

下面是我使用curl完成此操作的函数:

<?php
function get_url_data($url, $timeout = 5){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,            $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT,  $timeout );
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    $response = curl_exec($ch);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $body = substr($response, $header_size);
    $headers  = explode( "'n", $header );
    $code = 0;
    if(isset($headers[0])){
        if(preg_match('/[0-9]{3}/', $headers[0], $matches)){
            $code = $matches[0];
        }
    }
    return ['code' => $code, 'headers' => $headers, 'body' => $body];
}
echo '<pre>';
$timeout = 5;
$url = "http://www.ebay.com";
$results = get_url_data($url, $timeout);
print_r($results);
echo '</pre>';

示例经过测试,运行良好,请欣赏。