簡體   English   中英

從數據庫中卷曲ping URL並在網頁上發布時間

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

因此,我目前正在嘗試ping數據庫表中的URL,如果ping成功,則應該在網頁上發布該URL的'LIVE''DOWN'狀態,在以這種方式嘗試之前,我先對其進行了設置作為與fsocket一起工作的php數組。

目前,當我使用如下所示的代碼時,它確實從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';

那條線不應該在那兒。 如果在其中,則必須使用雙引號才能起作用。

單引號和雙引號在php中的工作方式非常不同。 如果將$ 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';

?>

第一個示例將按預期工作,但是第二個示例將按字面輸出“ $ strFirstName”而不是“ John”

由於您只需要確定某個特定的URL是否處於活動狀態,我很想使用get_headers相當快

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

或者,將其放在一個函數中

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 );

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM