本文介绍了获取标头响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我整理的PHP脚本的一部分。基本上,域($ domain1)在表单中定义,并根据服务器的响应代码显示不同的消息。但是,我遇到了让它运转的问题。 3位数的响应代码是我感兴趣的。
This is part of a PHP script I am putting together. Basically a domain ($domain1) is defined in a form and a different message is displayed, based on the response code from the server. However, I am having issues getting it to work. The 3 digit response code is all I am interested in.
这是我到目前为止所拥有的:
Here is what I have so far:
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3);
foreach ($get_http_response_code as $gethead) {
if ($gethead == 200) {
echo "OKAY!";
} else {
echo "Nokay!";
}
}
}
推荐答案
$domain1 = 'http://google.com';
function get_http_response_code($domain1) {
$headers = get_headers($domain1);
return substr($headers[0], 9, 3);
}
$get_http_response_code = get_http_response_code($domain1);
if ( $get_http_response_code == 200 ) {
echo "OKAY!";
} else {
echo "Nokay!";
}
这篇关于获取标头响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!