Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No need to use CURL, <code>file_get_contents($url);</code> will return false if the request fails (any other HTTP code other than 2xx), which might be more useful for what you're trying to do, an example:</p> <pre><code>function urlExists($url) { return (bool) @file_get_contents($url); } </code></pre> <p>Will return true if the URL returns useful content, false otherwise.</p> <hr> <p><strong>EDIT</strong>: Here is a faster way (it only requests the headers) and <strong>the first byte</strong> instead of the whole page:</p> <pre><code>function urlExists($url) { return (bool) @file_get_contents($url, false, null, 0, 1); } urlExists('https://stackoverflow.com/iDontExist'); // false </code></pre> <hr> <p>However, in combination with <a href="https://stackoverflow.com/questions/2719098/using-php-to-determine-if-a-remote-file-has-been-replaced">your other question</a> it may be wiser to use something like this:</p> <pre><code>function url($url) { return @file_get_contents($url); } $content = url('https://stackoverflow.com/'); // request has failed (404, 5xx, etc...) if ($content === false) { // delete or store as "failed" in the DB } // request was successful else { $hash = md5($content); // md5() should be enough but you can also use sha1() // store $hash in the DB to keep track of changes } </code></pre> <p>Or if you're using PHP 5.1+ you only have to do:</p> <pre><code>$hash = @md5_file($url); </code></pre> <p><code>$hash</code> will be false when the URL fails to load, otherwise it will return the MD5 hash of the contents.</p> <p>Graciously <a href="https://stackoverflow.com/questions/2719098/using-php-to-determine-if-a-remote-file-has-been-replaced/2719129#2719129">stolen from @Jamie</a>. =)</p> <p>This way you only have to make one request instead of two. =)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload