In the code below the get_status() method in my website class returns 1 instead of true or false like I want it to. Can anyone tell me why please? I think it's probably a mistake in my class, I'm not sure if this line of code is good practice within the get_status() method?
$httpcode = $this->get_httpcode();
When I echo $siteUp it is always 1 whether I put the url as http://www.google.com or http://www.dsfdsfsdsdfsdfsdf.com
I am fairly new to object orientated php and this is my first time teaching myself classes and this is an example I'm building to learn oop. It's desigened to check a website status and say whether it's up or down based on the httpcode.
Any tips you have in why this isn't working would be greatly recieved. Thanks in advance!
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;