Автор Сообщение
admin
СообщениеДобавлено: Ср Сен 26, 2018 5:40 pm    Заголовок сообщения: Проверка доступности URL

Ссылка на источник.

Код:

Код:
<?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
 *
 * @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 );
  if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
    return true;
  } else {
    // return $http_code;, possible too
    return false;
  }
  curl_close( $ch );
}
 
$website = "www.example.com";
if( !url_test( $website ) ) {
  echo $website ." is down!";
}
else { echo $website ." functions correctly."; }
?>

Powered by phpBB © 2001,2005 phpBB Group