重定向后的 HTTP 响应代码

2024-04-23

有一个到服务器的信息重定向,一旦来自服务器的响应,我想检查 HTTP 代码以抛出异常(如果有任何以 4XX 开头的代码)。为此,我需要知道如何从标头中仅获取 HTTP 代码?这里还涉及到服务器的重定向,所以我担心curl 对我来说没有用。

到目前为止我已经尝试过这个解决方案 https://stackoverflow.com/questions/3408049/getting-header-response-code但它非常慢并且在我的情况下会导致脚本超时。我不想增加脚本超时时间并等待更长的时间来获取 HTTP 代码。

预先感谢您的任何建议。


你的方法与get_headers请求第一个响应行将返回重定向的状态代码(如果有)and更重要的是,它将执行 GET 请求来传输整个文件。

您只需要一个 HEAD 请求,然后解析标头并返回last状态码。以下是执行此操作的代码示例,它使用$http_response_header代替get_headers,但数组的格式是相同的:

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

$options['http'] = array(
    'method' => "HEAD",
    'ignore_errors' => 1,
);

$context = stream_context_create($options);

$body = file_get_contents($url, NULL, $context);

$responses = parse_http_response_header($http_response_header);

$code = $responses[0]['status']['code']; // last status code

echo "Status code (after all redirects): $code<br>\n";

$number = count($responses);

$redirects = $number - 1;

echo "Number of responses: $number ($redirects Redirect(s))<br>\n";

if ($redirects)
{
    $from = $url;

    foreach (array_reverse($responses) as $response)
    {
        if (!isset($response['fields']['LOCATION']))
            break;
        $location = $response['fields']['LOCATION'];
        $code = $response['status']['code'];

        echo " * $from -- $code --> $location<br>\n";
        $from = $location;
    }
    echo "<br>\n";
}

/**
 * parse_http_response_header
 *
 * @param array $headers as in $http_response_header
 * @return array status and headers grouped by response, last first
 */
function parse_http_response_header(array $headers)
{
    $responses = array();
    $buffer = NULL;
    foreach ($headers as $header)
    {
        if ('HTTP/' === substr($header, 0, 5))
        {
            // add buffer on top of all responses
            if ($buffer) array_unshift($responses, $buffer);
            $buffer = array();

            list($version, $code, $phrase) = explode(' ', $header, 3) + array('', FALSE, '');

            $buffer['status'] = array(
                'line' => $header,
                'version' => $version,
                'code' => (int) $code,
                'phrase' => $phrase
            );
            $fields = &$buffer['fields'];
            $fields = array();
            continue;
        }
        list($name, $value) = explode(': ', $header, 2) + array('', '');
        // header-names are case insensitive
        $name = strtoupper($name);
        // values of multiple fields with the same name are normalized into
        // a comma separated list (HTTP/1.0+1.1)
        if (isset($fields[$name]))
        {
            $value = $fields[$name].','.$value;
        }
        $fields[$name] = $value;
    }
    unset($fields); // remove reference
    array_unshift($responses, $buffer);

    return $responses;
}

欲了解更多信息,请参阅:首先使用 PHP Streams 进行 HEAD http://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/,最后包含如何执行 HEAD 请求的示例代码get_headers以及。

有关的:如何使用 PHP 检查远程文件是否存在? https://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

重定向后的 HTTP 响应代码 的相关文章

随机推荐