суббота, 3 августа 2013 г.

PHP: Check availability of a site

PHP: Check availability of a site



  1. function checkUrl($url) {
  2. $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0";
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  7. curl_setopt($ch, CURLOPT_VERBOSE, false);
  8. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  9. $page = curl_exec($ch);
  10. $resp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  11. curl_close($ch);
  12. if (($resp >= 200) && ($resp < 310))
  13. return true;
  14. else
  15. return false;
  16. }
  17.  
  18. if (checkUrl("http://example"))
  19. echo "Website is available";
  20. else
  21. echo "Website is not available<br>";
  22.  

Комментариев нет:

Отправить комментарий

Постоянные читатели

Популярные сообщения