How can I check if a URL exists via PHP?

前端 未结 22 1197
天涯浪人
天涯浪人 2020-11-22 04:13

How do I check if a URL exists (not 404) in PHP?

相关标签:
22条回答
  • 2020-11-22 04:56

    Here is a solution that reads only the first byte of source code... returning false if the file_get_contents fails... This will also work for remote files like images.

     function urlExists($url)
    {
        if (@file_get_contents($url,false,NULL,0,1))
        {
            return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 04:58

    One thing to take into consideration when you check the header for a 404 is the case where a site does not generate a 404 immediately.

    A lot of sites check whether a page exists or not in the PHP/ASP (et cetera) source and forward you to a 404 page. In those cases the header is basically extended by the header of the 404 that is generated. In those cases the 404 error not in the first line of the header, but the tenth.

    $array = get_headers($url);
    $string = $array[0];
    print_r($string) // would generate:
    
    Array ( 
    [0] => HTTP/1.0 301 Moved Permanently 
    [1] => Date: Fri, 09 Nov 2018 16:12:29 GMT 
    [2] => Server: Apache/2.4.34 (FreeBSD) LibreSSL/2.7.4 PHP/7.0.31 
    [3] => X-Powered-By: PHP/7.0.31 
    [4] => Set-Cookie: landing=%2Freed-diffuser-fig-pudding-50; path=/; HttpOnly 
    [5] => Location: /reed-diffuser-fig-pudding-50/ 
    [6] => Content-Length: 0 
    [7] => Connection: close 
    [8] => Content-Type: text/html; charset=utf-8 
    [9] => HTTP/1.0 404 Not Found 
    [10] => Date: Fri, 09 Nov 2018 16:12:29 GMT 
    [11] => Server: Apache/2.4.34 (FreeBSD) LibreSSL/2.7.4 PHP/7.0.31 
    [12] => X-Powered-By: PHP/7.0.31 
    [13] => Set-Cookie: landing=%2Freed-diffuser-fig-pudding-50%2F; path=/; HttpOnly 
    [14] => Connection: close 
    [15] => Content-Type: text/html; charset=utf-8 
    ) 
    
    0 讨论(0)
  • 2020-11-22 04:59

    All above solutions + extra sugar. (Ultimate AIO solution)

    /**
     * Check that given URL is valid and exists.
     * @param string $url URL to check
     * @return bool TRUE when valid | FALSE anyway
     */
    function urlExists ( $url ) {
        // Remove all illegal characters from a url
        $url = filter_var($url, FILTER_SANITIZE_URL);
    
        // Validate URI
        if (filter_var($url, FILTER_VALIDATE_URL) === FALSE
            // check only for http/https schemes.
            || !in_array(strtolower(parse_url($url, PHP_URL_SCHEME)), ['http','https'], true )
        ) {
            return false;
        }
    
        // Check that URL exists
        $file_headers = @get_headers($url);
        return !(!$file_headers || $file_headers[0] === 'HTTP/1.1 404 Not Found');
    }
    

    Example:

    var_dump ( urlExists('http://stackoverflow.com/') );
    // Output: true;
    
    0 讨论(0)
  • 2020-11-22 04:59

    Other way to check if a URL is valid or not can be:

    <?php
    
      if (isValidURL("http://www.gimepix.com")) {
          echo "URL is valid...";
      } else {
          echo "URL is not valid...";
      }
    
      function isValidURL($url) {
          $file_headers = @get_headers($url);
          if (strpos($file_headers[0], "200 OK") > 0) {
             return true;
          } else {
            return false;
          }
      }
    ?>
    
    0 讨论(0)
  • 2020-11-22 05:00

    The best and simplest answer so far using get_headers() The best thing to check for string "200 ok". its far better than to check

    $file_headers = @get_headers($file-path);
    $file_headers[0];
    

    because sometime the array key numbers varies. so best thing is to check for "200 ok". Any URL which is up will have "200 ok" anywhere in get_headers() response.

    function url_exist($url) {
            $urlheaders = get_headers($url);
            //print_r($urlheaders);
            $urlmatches  = preg_grep('/200 ok/i', $urlheaders);
             if(!empty($urlmatches)){
               return true;
             }else{
               return false;
             }
    }
    

    now check the function if true or false

    if(url_exist(php-url-variable-here)
      URL exist
    }else{
      URL don't exist
    }
    
    0 讨论(0)
  • 2020-11-22 05:00

    kind of an old thread, but.. i do this:

    $file = 'http://www.google.com';
    $file_headers = @get_headers($file);
    if ($file_headers) {
        $exists = true;
    } else {
        $exists = false;
    }
    
    0 讨论(0)
提交回复
热议问题