Skip to content

Instantly share code, notes, and snippets.

@Alanaktion
Created September 29, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alanaktion/70bf152570236295774b to your computer and use it in GitHub Desktop.
Save Alanaktion/70bf152570236295774b to your computer and use it in GitHub Desktop.
PHP Ping API
<?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