How do I check if a URL exists (not 404) in PHP?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
When figuring out if an url exists from php there are a few things to pay attention to:
Keep in mind that whatever method you use, it takes time to wait for a response.
All code might (and probably will) halt untill you either know the result or the requests have timed out.
For example: the code below could take a LONG time to display the page if the urls are invalid or unreachable:
The functions below could be helpfull, you probably want to modify them to suit your needs:
All above solutions + extra sugar. (Ultimate AIO solution)
Example:
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.
Here:
From here and right below the above post, there's a curl solution: