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'

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?