newb Posted June 21, 2009 Share Posted June 21, 2009 I want to be able to check if a URL is live or not. Basically I want to make a function I can call and supply the URL, and it checks to see if the URL is currently up and running or not. Can this be done in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/ Share on other sites More sharing options...
chmpdog Posted June 21, 2009 Share Posted June 21, 2009 the file_exists function works. just type the url in it (with the http://) Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-860917 Share on other sites More sharing options...
newb Posted June 22, 2009 Author Share Posted June 22, 2009 file_exists function does not work on remote server locations... Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-860945 Share on other sites More sharing options...
chmpdog Posted June 22, 2009 Share Posted June 22, 2009 huh.. I guess your right. :'[ Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861027 Share on other sites More sharing options...
Ken2k7 Posted June 22, 2009 Share Posted June 22, 2009 Use cURL. Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861050 Share on other sites More sharing options...
chmpdog Posted June 22, 2009 Share Posted June 22, 2009 here it is: function url_exists($url) { if(!strstr($url, "http://")) { $url = "http://".$url; } $fp = @fsockopen($url, 80); if($fp === false) { return false; } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861058 Share on other sites More sharing options...
newb Posted June 22, 2009 Author Share Posted June 22, 2009 here it is: function url_exists($url) { if(!strstr($url, "http://")) { $url = "http://".$url; } $fp = @fsockopen($url, 80); if($fp === false) { return false; } return true; } that function is no good to me because it only checks if the webserver is up and running. i want to check if a specific html or other document is on the remote webserver.. thanks for the help though. Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861529 Share on other sites More sharing options...
thebadbad Posted June 22, 2009 Share Posted June 22, 2009 If you've got allow_url_fopen set to On, you can use file_get_contents(). It will return boolean false if the remote file isn't found (and spawn a warning). Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861545 Share on other sites More sharing options...
MadTechie Posted June 22, 2009 Share Posted June 22, 2009 This works with file and domains <?php $url = "http://www.phpfreaks.com"; if( !is_numeric(xfile_404($url)) ) { echo "Found: $url<br>"; } function xfile_404($file){ $file = ereg_replace(' +', '%20', trim($file)); if(substr($file, 0, 7) !== "http://"){ $file = "http://" . $file; } $domain = substr($file, 7); $domain_ext = strtoupper(array_pop(explode('.', substr($domain, 0, strpos($domain, '/'))))); $file_headers = @get_headers($file); if(!$file_headers){ return 3; break; }if($file_headers[0] == 'HTTP/1.1 404 Not Found') { return 404; }else{ return ereg_replace('%20', ' ', $file); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861558 Share on other sites More sharing options...
flyhoney Posted June 22, 2009 Share Posted June 22, 2009 Here is a library I wrote awhile back to accomplish this (it's probably based on the Kohana lib) Use it like this: <?php if (remote::status('http://www.google.com') == 200) { // url is alive } And here is the code: <?php class remote { /** * Determines the HTTP status of a URL * * @param string url * @return mixed HTTP status (int) or FALSE (bool) on failure */ public static function status($url) { if (strpos($url, '://') === FALSE) { $url = 'http://'.$url; } if (FALSE and function_exists('get_headers')) { if (($headers = get_headers($url)) !== FALSE) { list($protocol, $status, $name) = explode(' ', $headers[0]); } } else { $url = parse_url($url); $url['path'] = (empty($url['path'])) ? '/' : $url['path']; if (($fp = fsockopen($url['host'], 80, $errno, $errstr, 10)) === FALSE) { return FALSE; } fwrite($fp, 'HEAD '.$url['path'].' HTTP/1.0'."\r\n"); fwrite($fp, 'Host: '.$url['host']."\r\n"); fwrite($fp, 'Connection: close'."\r\n\r\n"); while (!feof($fp)) { $header = trim(fgets($fp, 512)); if (preg_match('#^HTTP/1\.[01] (\d{3})#', $header, $matches)) { $status = $matches[1]; break; } } fclose($fp); } return isset($status) ? (int)$status : FALSE; } } Quote Link to comment https://forums.phpfreaks.com/topic/163176-if-external-url-exists/#findComment-861560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.