重定向后的HTTP响应代码


HTTP response code after redirect

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

到目前为止,我已经尝试了这个解决方案,但它非常慢,并在我的情况下创建脚本超时。我不想增加脚本超时时间,等待更长时间只是为了获得HTTP代码。

提前感谢您的建议

使用get_headers并请求第一个响应行的方法将返回重定向(如果有的话)的状态码更重要的是,它将执行一个GET请求,该请求将传输整个文件。

您只需要一个HEAD请求,然后解析报头并返回最后一个状态码。下面是这样做的代码示例,它使用$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;
}

有关更多信息,请参阅:HEAD首先与PHP流,在最后它包含示例代码,您可以如何与get_headers一起做HEAD请求。

相关:如何使用PHP检查远程文件是否存在?

类似于

  $ch = curl_init(); 
  $httpcode = curl_getinfo ($ch, CURLINFO_HTTP_CODE );

你应该尝试HttpEngine类。希望对你有帮助。

,

编辑
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $your_agent_variable);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $your_referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode ...)

您找到的解决方案看起来不错。如果服务器不能及时向你发送http头,你的问题是另一个服务器坏了或负载很重。