Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ping site and return result in PHP

Tags:

php

ping

I'd like to create a small IF procedure that will check if Twitter is available (unlike now, for example), and will return true or false.

Help :)

like image 835
Tomer Lichtash Avatar asked Aug 06 '09 14:08

Tomer Lichtash


1 Answers

function urlExists($url=NULL)   {       if($url == NULL) return false;       $ch = curl_init($url);       curl_setopt($ch, CURLOPT_TIMEOUT, 5);       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);       $data = curl_exec($ch);       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);       curl_close($ch);       if($httpcode>=200 && $httpcode<300){           return true;       } else {           return false;       }   }   

This was grabbed from this post on how to check if a URL exists. Because Twitter should provide an error message above 300 when it is in maintenance, or a 404, this should work perfectly.

like image 195
Tyler Carter Avatar answered Sep 21 '22 09:09

Tyler Carter