Reputation: 377
I understand how classes are useful, but don't know what's the proper way to create ones that have prepared statements. I've started making a class and I want a method called isOnline that returns if the url is online.
// This is the code I'm trying to make a class.
global $db;
$stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url");
$stmt->bind_param("i", $_SESSION['admin_id']);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php
while($row = $result->fetch_array())
{
$url = $row['url'];
$site = strtolower($url);
// check if is online
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
if(!$statushttp == 0){
}else{
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
}
echo "$url with $statushttp <br>";
// email person here.
}
$stmt->free_result();
Upvotes: 0
Views: 33
Reputation: 1955
Not sure what you are doing to determine if a site is online or not but as far as creating a class to handle this. You should use something like this. If you can explain what you are doing to determine if a site is up or not I can update the class.
class SomeClassNameHere{
//this is the constructor of the class. This is ran when you create it
public SomeClassNameHere(){
}
//this is a public function that you can call
public function isOnline(){
//this is the first step to breaking up this into smaller peaces
//I moved your section of code that gets the urls from the database
//into a function that returns the rows from the database below.
$rows = getUrls();
while($row = $result->fetch_array()){
$site = strtolower($row['url']);
}
// check if is online
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
if(!$statushttp == 0){
}else{
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://'.$site ) );
$headers = explode( "\n", curl_exec( $curl ) );
$statushttp = $headers[0];
}
//I wouldn't echo out the results here,
//I would have this function return a true or false and do the display
//somewhere else
//like this
if(statushttp == whateverIsTrue){ //remember to edit this
return true;
}else{
return false;
}
}
//this has the possibility to return more than one row. You will need to
//change your function to handle this.
public function getUrls(){
$stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url");
$stmt->bind_param("i", $_SESSION['admin_id']);
$stmt->execute();
return $stmt->get_result();
}
}
To call this class you would want to do something like this.
//this is creating an instance of the class
$foo= new SomeClassNameHere();
//this will call the isOnline function of that class and return true or false
$siteOnline = $foo->isOnline();
if($siteOnline){
echo "Yeah!";
}else{
echo "Sad:-(";
}
Upvotes: 1