2012-08-03 33 views
0

我正在用etsy的开放源代码statsd library设置我的PHP脚本报告。 我基于他们给出的示例建立了连接,但是我遇到了一个问题,它似乎fsockopen忽略了try/catch并正在打印其错误。当statsd服务器启动并运行无法让PHP fsockopen忽略错误

一切正常,盛大 - 但如果它下来或PHP脚本无法连接到它:

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: 
Name or service not known 

我甚至试图在的fsockopen前面添加@,但没有骰子。这似乎也完全无视超时设置,因为它需要大约20秒返回错误:

 try { 
      $host = 'stats.thisserverisdown.com'; 
      $port = '8125'; 
      if ($fp = @fsockopen("udp://$host", $port, $errno, $errstr, 1)) { 
       if (!(get_resource_type($fp) == 'stream')) { return false; } 
       stream_set_timeout($fp,1); 
       stream_set_blocking($fp,false); 
       foreach ($sampledData as $stat => $value) { 
        @fwrite($fp, "$stat:$value"); 
       } 
       @fclose($fp); 
      } 
      return true; 
     } catch (Exception $e) { 
      return false; 
     } 

回答

1

fsockopen不引发错误。由于fsockopen必须解析您提供的主机名,因此它会调用getaddinfo(),这是失败的。

尝试提供一个IP地址,或者:

fsockopen(@"udp://$host", ...