Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Мониторинг сети с помощью fsockopen()
Форумы портала PHP.SU » » Работа с сетью » Мониторинг сети с помощью fsockopen()

Страниц (1): [1]
 

1. shadow_kan - 05 Марта, 2015 - 18:23:15 - перейти к сообщению
Доброго времени.

Есть функция для проверки хоста на доступность, выглядит так:
PHP:
скопировать код в буфер обмена
  1.  
  2.         function ping($host, $port = 80, $timeout = 6, $id = null) {
  3.  
  4.                 @error_reporting(0);
  5.                 @ini_set("display_errors", 'Off');
  6.  
  7.                 if (!$host)
  8.                         die('Error: no IP or IP is BAD');
  9.  
  10.                 $start          = microtime(true);
  11.  
  12.                 if (!$connect = @fsockopen($host, $port, &$errno, &$errstr, $timeout)) {
  13.                         $latency = null;
  14.                 } else {
  15.                         fclose($connect);
  16.                         $latency = microtime(true) - $start;
  17.                         //$latency = round($latency * 1000);
  18.                         $latency = 1;
  19.                 }
  20.  
  21.                 return
  22.                         array(
  23.                                 $id,
  24.                                 $latency
  25.                         );
  26.  
  27.         }
  28.  
  29. ping('54.44.55.22', 985, 6, 1);
  30.  
  31.  


Все бы хорошо, и все работает но если хост недоступен, тогда выдает ошибку :
<b>Warning</b>: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: unable to connect to 54.44.55.22:985 (Connection refused)

Собственно как избавится от ошибки?
2. shadow_kan - 05 Марта, 2015 - 22:05:15 - перейти к сообщению
Решил, возможно костыль но все же как умею...
PHP:
скопировать код в буфер обмена
  1.  
  2. function ping($host, $port = 80, $timeout = 6, $id = null) {
  3.  
  4.                 @error_reporting(0);
  5.                 @ini_set("display_errors", 'Off');
  6.                 ob_start();
  7.  
  8.                 if (!$host)
  9.                         die('Error: no IP or IP is BAD');
  10.  
  11.                 $start          = microtime(true);
  12.  
  13.                 if (!$connect = @fsockopen($host, $port, &$errno, &$errstr, $timeout)) {
  14.                         ob_end_clean();
  15.                         $latency = null;
  16.                 } else {
  17.                         fclose($connect);
  18.                         $latency = microtime(true) - $start;
  19.                         //$latency = round($latency * 1000);
  20.                         $latency = 1;
  21.                 }
  22.  
  23.                 return
  24.                         array(
  25.                                 $id,
  26.                                 $latency
  27.                         );
  28.  
  29.         }
  30.  
  31. ping('54.44.55.22', 985, 6, 1);
  32.  

 

Powered by ExBB FM 1.0 RC1