我正在使用以下代码ping IP地址(由https://www.saotn.org/php-curl-check-website-availability/提供)
<?php
/**
* PHP/cURL function to check a web site status. If HTTP status is not 200 or 302, or
* the requests takes longer than 10 seconds, the website is unreachable.
*
* Follow me on Twitter: @HertogJanR
* Send your donation through https://www.paypal.me/jreilink. Thanks!
*
* @param string $url URL that must be checked
*/
function url_test( $url ) {
$timeout = 10;
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
$http_respond = curl_exec($ch);
$http_respond = trim( strip_tags( $http_respond ) );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
echo $http_code;
if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
return true;
} else {
// return $http_code;, possible too
return false;
}
curl_close( $ch );
}
$website = "192.168.X.X";
if( !url_test( $website ) ) {
echo $website ." is down!";
}
else { echo $website ." functions correctly."; }
?>
其中192.168.X.X是我的默认网关。
当我通过本地主机运行它时,它说192.168.X.X正常运行。但是,当我在我的网站000webhostapp.com上运行它时,它等待10秒钟,并说192.168.X.X关闭! http代码为0的代码。关于出什么问题以及如何解决的任何想法?
答案 0 :(得分:0)
您没有发送ping操作,而是在进行cURL,因此请检查网络服务器是否已启动并正在运行。
它在本地工作,因为可以从内部网络访问管理界面。
要进行实际的ping操作,其他人已经为您完成了工作:
https://github.com/geerlingguy/Ping
$host = '106.x.x.x';
$ping = new Ping($host);
$latency = $ping->ping();
if ($latency !== false) {
print 'Latency is ' . $latency . ' ms';
} else {
print 'Host could not be reached.';
}