Jump to content

If External URL Exists?


newb

Recommended Posts

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.