I'm making a simple httpcode monitor as an example as I'm trying to teach myself object orientated PHP. I have written the code below but would be interested if it can be improved. In particular I'm not sure this line:
$httpcode = $website->get_httpcode($url);
This seems not very efficent to me as I have to to pass the url back to the website class right after I have already done just that two lines above. Shouldn't it already remember it's url? That might be a dumb questions but in my mind I've already made an object called website and given it a url. Then if I want the httpcode of the url then I feel there should be someway of it remembering it's url which I passed two lines above?
Any other code improvements would be appreciated as well. Thanks in advance.
class website {
var $url;
function __construct($url) {
$this->url = $url;
}
public function get_url() {
return $this->url;
}
public function get_httpcode($url) {
//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,$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;
}
}
// 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($url);