Php get_status()函数返回1而不是true或false,为什么?
在下面的代码中,我的网站类中的get_status()方法返回1,而不是像我希望的那样返回true或false。谁能告诉我为什么?我认为这可能是我的类中的一个错误,我不确定这行代码是否是get_status()方法中的良好实践 $httpcode=$this->get_httpcode() 当我回显$siteUp时,无论我将url设置为或,它总是1 我对面向对象的php相当陌生,这是我第一次自学课程,这是我学习oop的一个例子。它的目的是检查一个网站的状态,并根据httpcode判断它是向上还是向下 如果您有任何关于为什么这不起作用的建议,我们将不胜感激。提前谢谢Php get_status()函数返回1而不是true或false,为什么?,php,oop,class,Php,Oop,Class,在下面的代码中,我的网站类中的get_status()方法返回1,而不是像我希望的那样返回true或false。谁能告诉我为什么?我认为这可能是我的类中的一个错误,我不确定这行代码是否是get_status()方法中的良好实践 $httpcode=$this->get_httpcode() 当我回显$siteUp时,无论我将url设置为或,它总是1 我对面向对象的php相当陌生,这是我第一次自学课程,这是我学习oop的一个例子。它的目的是检查一个网站的状态,并根据httpcode判断它是向上还是
class website {
protected $url;
function __construct($url) {
$this->url = $url;
}
public function get_url() {
return $this->url;
}
public function get_httpcode() {
//get the http status code
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$this->url);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
$page=curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpcode;
}
public function get_status() {
$httpcode = $this->get_httpcode();
if ($httpcode>=200 && $httpcode<400) {
$siteUp = true;
} else {
$siteUp = false;
}
return $siteUp;
}
}
// create an instance of the website class and pass the url
$website = new website("http://www.google.com");
$url = $website->get_url();
$httpcode = $website->get_httpcode();
$siteUp = $website->get_status();
echo "site up is set to: " . $siteUp;
班级网站{
受保护的$url;
函数构造($url){
$this->url=$url;
}
公共函数get_url(){
返回$this->url;
}
公共函数get_httpcode(){
//获取http状态代码
$agent=“Mozilla/4.0(兼容;MSIE 5.01;Windows NT 5.0)”;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$this->URL);
curl_setopt($ch,CURLOPT_USERAGENT,$agent);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch,CURLOPT_超时,10);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
卷曲设置($ch,卷曲设置,3);
$page=curl\u exec($ch);
$httpcode=curl\u getinfo($ch,CURLINFO\u HTTP\u代码);
卷曲关闭($ch);
返回$httpcode;
}
公共函数get_status(){
$httpcode=$this->get_httpcode();
如果($httpcode>=200&&$httpcodeget_url();
$httpcode=$website->get_httpcode();
$siteUp=$website->get_status();
echo“站点设置为:”.$siteUp;
您返回的是一个布尔值,它是TRUE
或FALSE
(但不是单词TRUE或FALSE)
您可以返回字符串,也可以在将其附加到echo语句之前对其进行转换:
例如:
public function get_status() {
$httpcode = $this->get_httpcode();
if ($httpcode>=200 && $httpcode<400) {
$siteUp = 'true';
} else {
$siteUp = 'false';
}
return $siteUp;
}
作为一个简单的练习,请尝试运行以下代码并亲自查看:
<?php
echo true;
?>
1是PHP将“true”转换为字符串的方式
<?php echo true; ?>
将使其成为一个字符串供您显示…但您无法对其进行真实性测试,因为“true”和“false”都是有效字符串,将为您提供布尔值true。避免打印出
true
/false
。使用if语句查看它实际返回的内容。
您希望打印什么$siteUp
if($siteUp) {
echo "Up";
} else {
echo "Down";
}
您是否已尝试先回显
$httpcode
?为什么不返回$httpcode>=200&&$httpcode除了http代码之外,还应检查curl\u exec
的值(在您的情况下,变量$page
)。如果失败,$page
的值将为false,
<?php echo true; ?>
$siteUp = $website->get_status() ? "true" : "false";
if($siteUp) {
echo "Up";
} else {
echo "Down";
}