Jump to content

If External URL Exists?


newb

Recommended Posts

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?

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}
}
?>

Link to comment
Share on other sites

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;
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.