Hi I'm new to this forum so sorry if I do it wrong!

I've got a list of 160 URL's in a db table and the rows are shown like:

<?php

require_once "config/config.php";

$sql = "SELECT * FROM deployments";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['server'] ?></td>
<td> <?php echo $row['name'] ?></td>
<td> <?php echo $row['url'] ?></td>
<td> <?php echo $row['port'] ?></td>
<td><span class='badge badge-warning'>LIVE</span></td>
<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
</tr>
<?php
}
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

?>

Is it possible to use fsoc or curl to ping the URL's pulled from the db after made visible on the webpage with line:
<td> <?php echo $row['url'] ?></td>

I've tried this but I can't seem to get it to work, I had it working when I pulled the data from an array but I want the data to come from the db.

Thank you, and if I need to change something let me know and I will edit my post.

You might be able to run the system's ping with system() or backticks, or exec/shell_exec. You'd probably want to add "-c 1" to only do it once, and then check the result variable.

If you're looking for a specific socket, I love tcping but it's not available on many servers and you'd still have to use system & friends.

With all that in mind, here's a simple PHP one-liner for fsockopen:

// see if example.com is listening on the HTTP port
$up = is_resource(fsockopen('example.com', 80));

    Beanmen I've tried this but I can't seem to get it to work, I had it working when I pulled the data from an array but I want the data to come from the db.

    How was changing the source of the data changing the way you were using it? If you could put the data from the database into an array and then use the array would that make things workable?

    NogDog

    All websites I find pull the data from 1 website i.e.

    $website = "www.example.com"; << This is not one website but about 160, this data is pulled from a database
    if( !url_test( $website ) ) {
    echo $website ." is down!";
    }
    else { echo $website ." functions correctly."; }

    Weedpacket

    My old code looks like this:

    <?php
    						$tests = array
    						(
                                                        `array list`
    						);
    
    						// Set counter
    						$i = 1;
    
    						// For each test create a tr
    						foreach($tests as $test => $testProperties){
    
    							echo "<tr>";
    
    							// Display test ID
    							echo "<td>$i</td>";
    
    
    							echo "<td>".$testProperties[0]."</td>";
    							echo "<td>".$testProperties[1]."</td>";
    
    							if($socket =@ fsockopen($testProperties[1], 80, $errno, $errstr, 1)) {
    
    								echo "<td><span class='badge badge-success'>LIVE</span></td>";
    								fclose($socket);
    
    							}
    							else {
    
    								echo "<td><span class='badge badge-danger'>DOWN</span></td>";
    
    							}
    
    							echo "</tr>";
    
    							// Increase counter
    							$i++;
    
    						}
    
              ?>

    I changed this code to the following but the fsock suddenly didn't ping the servers any longer (this code isn't including the curl ping bit of code it's just the way the page is showing):

    <?php
    
    							require_once "config/config.php";
    
    							$sql = "SELECT * FROM deployments";
    							if($result = mysqli_query($link, $sql)){
    									if(mysqli_num_rows($result) > 0){
    										while($row = mysqli_fetch_array($result)){
    											?>
    													<tr>
    															<td> <?php echo $row['id'] ?></td>
    															<td> <?php echo $row['server'] ?></td>
    															<td> <?php echo $row['name'] ?></td>
    															<td> <?php echo $row['url'] ?></td>
    															<td> <?php echo $row['port'] ?></td>
    															<td><span class='badge badge-warning'>LIVE</span></td>
    															<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
    													</tr>
    							<?php
    											}
    											// Free result set
    											mysqli_free_result($result);
    									} else{
    											echo "No records matching your query were found.";
    									}
    							} else{
    									echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    							}
    
    							?>

    How do I make the fsock or curl ping all the servers shown in the database under the "url" table, as I can't get them to show.

    Beanmen I changed this code to the following but the fsock suddenly didn't ping the servers any longer

    That's a completely different part of the code.

    Beanmen (this code isn't including the curl ping bit of code it's just the way the page is showing):

    Since it's the curl ping bit of code that is causing the problem, maybe that would be more useful to see than something totally unrelated.

    Like I said: what is stopping you from simply querying the database to make your $tests array and then using that array?

    Weedpacket

    I know it is completely different because I changed the view of the website, so the old code with the array was working fine, all the servers were shown in this list, I changed the website and added a add server button that adds the data to a db, this data is shown on the website, but I cant get the curl to ping the output data, I know I'm not making sense and that I'm wrong somewhere but I can't see what goes wrong.

    In my head the curl pings the output from the table, not the data on the table before that data is shown as a output on the website in a <td> tag.

    <?php
    
    							require_once "config/config.php";
    
    							$sql = "SELECT * FROM deployments";
    							if($result = mysqli_query($link, $sql)){
    									if(mysqli_num_rows($result) > 0){
    										while($row = mysqli_fetch_array($result)){
    											?>
    													<tr>
    															<td> <?php echo $row['id'] ?></td>
    															<td> <?php echo $row['server'] ?></td>
    															<td> <?php echo $row['name'] ?></td>
    															<td> <?php echo $row['url'] ?></td>
    															<td> <?php echo $row['port'] ?></td>
    <?php
    	function url_test( $url ) {
      $timeout = 10;
      $ch = curl_init();
      curl_setopt ( $ch, CURLOPT_URL, $url );
      curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
      $http_respond = curl_exec($ch);
      $http_respond = trim( strip_tags( $http_respond ) );
      $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
      if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
        return true;
      } else {
        // return $http_code;, possible too
        return false;
      }
      curl_close( $ch );
    }
    
    $website = "www.www.com";
    if( !url_test( $website ) ) {
      echo "<td><span class='badge badge-danger'>DOWN</span></td>";
    }
    else { echo "<td><span class='badge badge-success'>LIVE</span></td>"; }
    
    ?>
    															<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
    													</tr>
    							<?php
    											}
    											// Free result set
    											mysqli_free_result($result);
    									} else{
    											echo "No records matching your query were found.";
    									}
    							} else{
    									echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    							}
    
    							?>

      Beanmen All websites I find pull the data from 1 website i.e.

      $website = "www.example.com"; << This is not one website but about 160, this data is pulled from a database
      if( !url_test( $website ) ) {
      echo $website ." is down!";
      }
      else { echo $website ." functions correctly."; }

      So...you take their code, and wrap it in a loop. The could be a foreach() loop on an array of URLs that you extract from your database, or a for() loop on a database fetch operation on each row of a DB query result set -- whichever way you prefer. Since you're already doing the latter here...

      while($row = mysqli_fetch_array($result)){
      

      ...just stick a call to the desired "ping" function right after that, assigning the result to a variable; and then in the HTML markup that follows, echo that result (or modified to whatever verbiage you want) at the desired point in the table output.

      NogDog

      I've got it working lol

                  <?php
      
      	require_once "config/config.php";
      
      	$sql = "SELECT * FROM deployments";
      	if($result = mysqli_query($link, $sql)){
      			if(mysqli_num_rows($result) > 0){
      				while($row = mysqli_fetch_array($result)){
      					?>
      							<tr>
      									<td> <?php echo $row['id'] ?></td>
      									<td> <?php echo $row['server'] ?></td>
      									<td> <?php echo $row['name'] ?></td>
      									<td> <?php echo $row['url'] ?></td>
      									<td> <?php echo $row['port'] ?></td>
      
      
      									<?php
      									  $url = $row['url'];
      									  $ch = curl_init($url);
      									  curl_setopt($ch, CURLOPT_NOBODY, true);
      									  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      									  curl_exec($ch);
      									  $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      									  curl_close($ch);
      									  if (200==$retcode) {
      									    echo "<td><span class='badge badge-success'>LIVE</span></td>";
      									  } else {
      									    echo "<td><span class='badge badge-danger'>DOWN</span></td>";
      									    $path_to_file = './emailtemplate.html';
      									    $file_contents = file_get_contents($path_to_file);
      									    $file_contents = str_replace("depplaceholder","$url",$file_contents);
      									    file_put_contents($path_to_file,$file_contents);
      
      									        $to = "";
      									        $subject = "$url down";
      									        $headers = "From: Deployment Monitor" . "\r\n";
      									        $headers .= 'MIME-Version: 1.0' . "\r\n";
      									        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      									        $message = file_get_contents('./emailtemplate.html');
      
      									      //	mail($to,$subject,$message,$headers);
      									  }
      									  ?>
      
      
      									<td><a href="config/remove.php?id=<?php echo $row['id']; ?>"><span class="fa fa-trash"></span></a></td>
      							</tr>
      	<?php
      					}
      					// Free result set
      					mysqli_free_result($result);
      			} else{
      					echo "No records matching your query were found.";
      			}
      	} else{
      			echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
      	}
      
      	?>

        Now we just need to get that indenting under control. 😉

        NogDog

        It's under control! haha I couldn't get the code to show as code on the page here, so I copied it from my php file (which doesn't look like this lol) and I guess it got a bit messed up.

          Write a Reply...