Created
September 29, 2014 16:28
-
-
Save Alanaktion/70bf152570236295774b to your computer and use it in GitHub Desktop.
PHP Ping API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!empty($_GET["host"])) { | |
$host=$_GET["host"]; | |
if(strpos($host,"/")!==false && ($h=parse_url($host,PHP_URL_HOST))!==false) | |
$host=$h; | |
$host=preg_replace("/(^\-)?[^0-9a-z\.]/i","",$host); | |
$result=shell_exec("ping -nc 1 -W 2000 $host"); | |
preg_match("/(?<=time=)[0-9\.]+/",$result,$m); | |
if(!empty($m[0])) | |
$out=array("online"=>true,"host"=>$host,"ping"=>$m[0]."ms"); | |
else | |
$out=array("online"=>false,"host"=>$host); | |
} else | |
$out=array("error"=>"Missing parameter `host`."); | |
header("Content-type: application/json"); | |
echo json_encode($out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment