DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Steve has posted 12 posts at DZone. View Full User Profile

Check The Existence Of A URL In PHP

07.29.2009
| 4373 views |
  • submit to reddit
        checkUrl allows up to 4 redirects. All Location headers are returned in the 'locations' key of the array (and Locations that begin with "/" are resolved to the last host.

function checkUrl($url) {
    $root = _getRoot($url);
    $ctx = stream_context_create(array(
       'http' => array(
            'max_redirects' => 5, // allows 4 redirects :/
            'timeout' => 5,
        )
    ));
    $fp = @fopen($url, 'r', false, $ctx);
    $ret['success'] = false;
    if (! $fp) {
        return $ret;
    }
    $ret['locations'] = array();
    $ret['metaData'] = stream_get_meta_data($fp);
    fclose($fp);
    // analyze HTTP headers
    foreach ($ret['metaData']['wrapper_data'] as $line) {
        if (preg_match('@^Location: (.*)$@i', $line, $m)) {
            if ($m[1][0] === '/') {
                // root-relative URI
                $m[1] = $root . $m[1];
            } elseif (strpos($m[1], '://') >= 4) {
                // full URL
                $root = _getRoot($m[1]);
            }
            $ret['locations'][] = $m[1];
        }
        if (preg_match('@^HTTP/1\\.[01] 200@i', $line, $m)) {
            $ret['success'] = true;
        }
    }
    return $ret;
}

function _getRoot($url) {
    list($proto, $url) = explode('://', $url, 2);
    list($host) = explode('/', $url, 2);
    return $proto . '://' . $host;
}

var_export(checkUrl('http://microsoft.com/ie'));

// outputs

array (
  'success' => true,
  'locations' => 
  array (
    0 => 'http://www.microsoft.com/ie',
    1 => 'http://www.microsoft.com/ie/',
    2 => 'http://www.microsoft.com/windows/internet-explorer/default.aspx',
  ),
  'metaData' => 
  array (
    'wrapper_data' => 
    array (
      ... abunch of HTTP headers
    ),
    'wrapper_type' => 'http',
    'stream_type' => 'tcp_socket/ssl',
    'mode' => 'r+',
    'unread_bytes' => 3478,
    'seekable' => false,
    'uri' => 'http://microsoft.com/ie',
    'timed_out' => false,
    'blocked' => true,
    'eof' => false,
  ),
)