doubeng3412 2019-01-28 15:29
浏览 21

curl ping数据库中的URL并在网页上发布时间

So I'm currently trying to ping urls in a db table that should then post the 'LIVE' or 'DOWN' status of that url on a web page if the ping was successful, Before I tried it this way I had it set up as a php array with fsocket which worked.

Currently when I use the code as shown below it does pull the data from the db table but the badge showing 'LIVE' or 'DOWN' constantly shows 'DOWN'.

<?php
require_once "config/config.php";

$sql = "SELECT * FROM deployments";
 if($result = mysqli_query($link, $sql)){
 if(mysqli_num_rows($result) > 0){
  echo "<tr>";
  echo "<th>Server</th>";
  echo "<th>Deployment</th>";
  echo "<th>URL</th>";
  echo "<th>Status</th>";
  echo "<th>Port</th>";
  echo "</tr>";
while($row = mysqli_fetch_array($result)){
  echo "<tr>";
  echo "<td>" . $row['server'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['url'] . "</td>";
   $url = $row['url'];
   $port = $row['port'];
   $url = '$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>";
}
  echo "<td>" . $row['port'] . "</td>";
  echo "</tr>";
}
// 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);
}
?>
  • 写回答

2条回答 默认 最新

  • douyan8772 2019-01-28 15:38
    关注

    $url='$url';

    That line shouldn't be in there. And if it is in there it needs to be double quotes to work.

    Single quotes and double quotes work very differently in php. Having a $variable wont work as you're expecting if it's surrounded by single quotes.

    <?php
    //Example of how php treats a string in double quotes
    $strFirstName="John";
    echo "$strFirstName";
    
    ?>
    
    <?php
    //Example of how php treats a string in single quotes
    $strFirstName="John";
    echo '$strFirstName';
    
    ?>
    

    The first example will work as expected, but the second one will literally output '$strFirstName' instead of 'John'

    评论

报告相同问题?