回答
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,因此應該完美地工作。
如果你需要SSL,然後看看這個帖子https:// stackoverflow。com/questions/4372710/php-curl-https – eballo 2017-10-02 14:58:29
這裏有一個:
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);
ping
幾乎可用於所有操作系統。所以你可以進行系統調用並獲取結果。
使用shell_exec:
<?php
$output = shell_exec('ping -c1 google.com');
echo "<pre>$output</pre>";
?>
你應該在Linux上使用'ping -c1 host'或其他東西。普通'ping主機'不會在那裏返回。 – Michas 2010-05-13 13:51:14
更好: 'if(0!=($ _result = \'ping -q -c1 google.com>/dev/null 2> &1 ; echo $?'')){ echo'Fail。'; }' – lucifurious 2013-02-27 17:20:43
另一種選擇(如果你需要/想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.';
}
用下面的函數使用的是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;
}
這是我使用的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; }
這可能會導致'ping:icmp open socket:Permission denied'。爲了解決這個問題,SELinux必須是Permissive – 2016-09-11 22:19:17
- 1. Java運行cURL並返回網站頁面結果
- 2. PHP Post form並返回xml結果
- 3. 使用PHP Ping IP地址並回顯結果
- 4. 返回循環結果PHP
- 5. php preg_match不返回結果
- 6. PHP返回循環結果
- 7. Post後返回結果PHP
- 8. PHP SNMP返回結果
- 9. PHP - 返回計數(*)結果
- 10. Ping一個網站地址返回一個IP
- 11. 在php中返回true,並在javascript中獲取結果
- 12. 使用php來ping一個網站
- 13. 函數在PHP中不返回結果
- 14. date返回錯誤的結果在php
- 15. 如何從Android活動ping一個網站並得到迴應?
- 16. 亞馬遜API未返回結果與亞馬遜網站結果對齊
- 17. 在PHP中搜索數組並返回結果
- 18. PHP - 在同一頁面搜索數據庫並返回結果
- 19. PHP - 獲取服務器ping一個訪客IP並返回ping毫秒
- 20. 返回結果
- 21. 返回結果
- 22. 存儲的prodecure結果總是返回-1在返回結果
- 23. 簡單的PHP網絡爬蟲提交表單並存儲返回的結果
- 24. 需要建立一個網址,並與返回的結果
- 25. LINQ羣組並返回所有結果
- 26. 動態添加wizardPages並返回結果
- 27. 沒有結果返回elseif PHP查詢
- 28. php查詢不返回結果
- 29. php mysqli_fetch_array返回$ name結果爲'name'
- 30. PHP PDO的execute()返回重複結果
,而不是平,不能你做一個HTTP請求? – palindrom 2009-08-06 14:05:24