Skip to content

Instantly share code, notes, and snippets.

@seanvree
Created January 21, 2018 17:22
Show Gist options
  • Save seanvree/87ff89e04bf6a57bf9506488c584007d to your computer and use it in GitHub Desktop.
Save seanvree/87ff89e04bf6a57bf9506488c584007d to your computer and use it in GitHub Desktop.
curl_ping
<?php
/**
* PHP/cURL function to check a web site status. If HTTP status is between 200 and 400,
* Generally all successes are in this range, the website is reachable.
*
*
* @param string $url URL that must be checked
*/
function urlExists($url) {
global $t;
global $k;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_FRESH_CONNECT, true);
curl_setopt($handle, CURLOPT_FORBID_REUSE, true);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($handle, CURLOPT_TCP_FASTOPEN, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 30);
curl_setopt($handle, CURLOPT_URL, $url);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$curlCode = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
if($httpCode >= 200 && $httpCode < 400 || $httpCode == 401 || $httpCode == 403 || $httpCode == 405 || $curlCode == 8 || $curlCode == 67 || $curlCode == 530 || $curlCode == 60 ) {
echo '<div class="col-lg-4">';
echo '<a class="servicetile" href="'. $k['link'] .'" target="_blank" style="display: block">';
echo '<div id="serviceimg">';
echo '<p><img id="'. strtolower($t) .'-service-img" src="assets/img/'. strtolower($k['image']) .'" style="height:85px" alt=""></p>';
echo '</div>';
echo '<div id="servicetitle">';
echo '<div class="servicetext">';
echo '<p>'. ucfirst($t) .'</p>';
echo '</div>';
echo '</div>';
echo '<p class="btnonline">Online</p>';
echo '</a>';
echo '</div>';
}
else {
function url_to_domain($url) {
$host = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
if (!$host)
$host = $url;
if (substr($host, 0, 4) == "www.")
$host = substr($host, 4);
if (strlen($host) > 50)
$host = substr($host, 0, 47) . '...';
return $host . ":" . $port . $path;
}
// $fp = fsockopen($url, $timeout = 3);
$fp = pfsockopen(url_to_domain($url));
if (!$fp) {
//echo "error: $errstr ($errno)<br />\n";
//echo CLOSED;
echo '<div class="col-lg-4">';
echo '<div id="serviceimg">';
echo '<p class="offline"><img id="'. strtolower($t) .'-service-img" src="assets/img/'. strtolower($k['image']) .'" style="height:85px" alt=""></p>';
echo '</div>';
echo '<div id="servicetitle">';
echo '<a class="servicetextoffline" href="'. $k['link'] .'" target="_blank" style="display: block">';
echo '<p>'. ucfirst($t) .'</p>';
echo '</a>';
echo '</div>';
echo '<p class="btnoffline">CLOSED</p>';
echo '</div>';
// fclose($fp);
}
else {
// $out .= "$url\r\n";
// $out .= "Connection: Close\r\n\r\n";
// fwrite ($fp, $out);
//echo OPEN;
echo '<div class="col-lg-4">';
echo '<a class="servicetile" href="'. $k['link'] .'" target="_blank" style="display: block">';
echo '<div id="serviceimg">';
echo '<p><img id="'. strtolower($t) .'-service-img" src="assets/img/'. strtolower($k['image']) .'" style="height:85px" alt=""></p>';
echo '</div>';
echo '<div id="servicetitle">';
echo '<div class="servicetext">';
echo '<p>'. ucfirst($t) .'</p>';
echo '</div>';
echo '</div>';
echo '<p class="btunknown">OPEN</p>';
echo '</a>';
echo '</div>';
fclose($fp);
}
}
curl_close($handle);
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment