2009-08-06 95 views

回答

34
function urlExists($url=NULL) 
{ 
    if($url == NULL) return false; 
    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $data = curl_exec($ch); 
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 
    if($httpcode>=200 && $httpcode<300){ 
     return true; 
    } else { 
     return false; 
    } 
} 

這是從this post抓起如何檢查是否存在URL。由於Twitter在維護時應提供高於300的錯誤消息,或404,因此應該完美地工作。

+0

如果你需要SSL,然後看看這個帖子https:// stackoverflow。com/questions/4372710/php-curl-https – eballo 2017-10-02 14:58:29

19

這裏有一個:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786

另:

function ping($host, $port, $timeout) { 
    $tB = microtime(true); 
    $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
    if (!$fP) { return "down"; } 
    $tA = microtime(true); 
    return round((($tA - $tB) * 1000), 0)." ms"; 
} 

//Echoing it will display the ping if the host is up, if not it'll say "down". 
echo ping("www.google.com", 80, 10); 
+8

這沒有什麼好的回報值。爲什麼不在失敗時返回0/false/null並且表示成功毫秒數的整數? – 2009-08-06 14:52:04

+2

@Philippe Gerber - 因爲我沒有寫出來,但這些都是很好的建議。 – karim79 2009-08-06 14:57:42

+4

Ping正在開發ICMP協議,沒有像'port'這樣的東西。您可以使用0個開放的tcp端口來ping主機。 – deejayy 2011-12-15 07:05:55

5

ping幾乎可用於所有操作系統。所以你可以進行系統調用並獲取結果。

8

使用shell_exec

<?php 
$output = shell_exec('ping -c1 google.com'); 
echo "<pre>$output</pre>"; 
?> 
+4

你應該在Linux上使用'ping -c1 host'或其他東西。普通'ping主機'不會在那裏返回。 – Michas 2010-05-13 13:51:14

+0

更好: 'if(0!=($ _result = \'ping -q -c1 google.com>/dev/null 2> &1 ; echo $?'')){ echo'Fail。'; }' – lucifurious 2013-02-27 17:20:43

6

另一種選擇(如果你需要/想ping通,而不是發送HTTP請求)是Ping class for PHP。我爲此寫了它,它允許您使用三種支持的方法之一來ping服務器(某些服務器/環境僅支持三種方法之一)。

用法示例:

require_once('Ping/Ping.php'); 
$host = 'www.example.com'; 
$ping = new Ping($host); 
$latency = $ping->ping(); 
if ($latency) { 
    print 'Latency is ' . $latency . ' ms'; 
} 
else { 
    print 'Host could not be reached.'; 
} 
0

用下面的函數使用的是socket_create只發送純ICMP數據包。我從那裏獲得了a user note的以下代碼。注:您必須運行以下內容作爲

雖然你不能把它放在一個標準的網頁上,你可以運行它作爲一個cron作業,並用結果填充數據庫。

因此,如果您需要監控一個網站,它是最適合的。

function twitterIsUp() { 
    return ping('twitter.com'); 
} 

function ping ($host, $timeout = 1) { 
    /* ICMP ping packet with a pre-calculated checksum */ 
    $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost"; 
    $socket = socket_create(AF_INET, SOCK_RAW, 1); 
    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); 
    socket_connect($socket, $host, null); 

    $ts = microtime(true); 
    socket_send($socket, $package, strLen($package), 0); 
    if (socket_read($socket, 255)) {  
     $result = microtime(true) - $ts; 
    } else { 
     $result = false; 
    } 
    socket_close($socket); 

    return $result; 
} 
0

這是我使用的PHP代碼,回答通常是這樣的:

 
    2 packets transmitted, 2 received, 0% packet loss, time 1089ms 

所以我用這樣的代碼:

 

    $ping_how_many = 2; 
    $ping_result = shell_exec('ping -c '.$ping_how_many.' bing.com'); 
    if(!preg_match('/'.$ping_how_many.' received/',$ping_result)){ 
     echo 'Bad ping result'. PHP_EOL; 
     // goto next1; 
    } 


+0

這可能會導致'ping:icmp open socket:Permission denied'。爲了解決這個問題,SELinux必須是Permissive – 2016-09-11 22:19:17