简体   繁体   English

从数据库中卷曲ping URL并在网页上发布时间

[英]curl ping URL from database and post up time on web page

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. 因此,我目前正在尝试ping数据库表中的URL,如果ping成功,则应该在网页上发布该URL的'LIVE''DOWN'状态,在以这种方式尝试之前,我先对其进行了设置作为与fsocket一起工作的php数组。

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' . 目前,当我使用如下所示的代码时,它确实从db表中提取了数据,但是显示'LIVE''DOWN'的标志始终显示'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);
}
?>

$url='$url'; $ 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. 单引号和双引号在php中的工作方式非常不同。 Having a $variable wont work as you're expecting if it's surrounded by single quotes. 如果将$ variable括在单引号中,将无法正常工作。

<?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' 第一个示例将按预期工作,但是第二个示例将按字面输出“ $ strFirstName”而不是“ John”

As you simply need to determine if a particular url is active or not I'd be tempted to use get_headers ~ it's pretty quick 由于您只需要确定某个特定的URL是否处于活动状态,我很想使用get_headers相当快

$url='https://www.google.com';
$headers = get_headers( $url, 1 );
$active = !empty( $headers );
if( $active ){/* do stuff */}

Or, put it in a function 或者,将其放在一个函数中

function get_status( $url ){
    $headers = @get_headers( $url, 1 );
    return !empty( $headers );
}

$url = 'http://www.holycatflap.com';

$status = get_status( $url ) ? sprintf( '"%s" is UP!',$url ) : sprintf( '"%s" DOWN',$url );
printf("<pre>%s</pre>", $status );

outputs: 输出:

"http://www.holycatflap.com" DOWN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM