HTTP响应代码后重定向
有一个重定向到服务器的信息,一旦响应来自服务器,我想检查HTTP代码抛出一个异常,如果有开始4XX任何代码。为此,我需要知道如何从头获取HTTP代码?此处也涉及到重定向到服务器,所以我害怕curl对我无用。HTTP响应代码后重定向
到目前为止,我已经尝试this solution但它是非常缓慢的,在我的情况下创建的脚本超时。我不想增加脚本超时时间,而只是为了获取HTTP代码而等待更长时间。
在此先感谢您的任何建议。
喜欢的东西:
$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 ...)
对不起,我不知道HttpEngine类。你能给我提供一些相关信息吗? –
没问题。那么,我不记得我在哪里上过这门课...所以我上传了这个:http://pastebin.ca/2083612 告诉我你是否需要任何帮助! –
我看着类文件,但不知道它将如何帮助这里。当我使CURLOPT_FOLLOWLOCATION为true来重定向时,它使用curl并使用curl,GET方法正在获取整个页面。 –
你找到了解决办法看起来不错。如果服务器无法及时向您发送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 first with PHP Streams,在结束它包含的代码示例如何能以get_headers
做HEAD请求。
相关:How can one check to see if a remote file exists using PHP?
相关:[PHP:get_headers临时设置stream_context](http://stackoverflow.com/questions/8429342/php-get-headers-set-temporary-stream-context) – hakre
cURL可以遵循重定向就好。您可以在文档,而不是担心你的假设在读它:http://php.net/manual/en/function.curl-setopt.php,寻找'CURLOPT_FOLLOWLOCATION' – Piskvor
我试着用卷曲CURLOPT_FOLLOWLOCATION选项。事情是它将页面提取到本地服务器,当我在从服务器获取的页面上输入用户名/密码时,服务器会在重定向时感到困惑。如何解决这个问题? –