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
I run some tests to see if links on my site are valid - alerts me to when third parties change their links. I was having an issue with a site that had a poorly configured certificate that meant that php's get_headers didn't work.
SO, I read that curl was faster and decided to give that a go. then i had an issue with linkedin which gave me a 999 error, which turned out to be a user agent issue.
I didn't care if the certificate was not valid for this test, and i didn't care if the response was a re-direct.
Then I figured use get_headers anyway if curl was failing....
Give it a go....
karim79's get_headers() solution didn't worked for me as I gotten crazy results with Pinterest.
Anyway, this developer demonstrates that cURL is way faster than get_headers():
http://php.net/manual/fr/function.get-headers.php#104723
Since many people asked for karim79 to fix is cURL solution, here's the solution I built today.
Let me explains the curl options:
CURLOPT_RETURNTRANSFER: return a string instead of displaying the calling page on the screen.
CURLOPT_SSL_VERIFYPEER: cUrl won't checkout the certificate
CURLOPT_HEADER: include the header in the string
CURLOPT_NOBODY: don't include the body in the string
CURLOPT_USERAGENT: some site needs that to function properly (by example : https://plus.google.com)
Additional note: In this function I'm using Diego Perini's regex for validating the URL before sending the request:
Additional note 2: I explode the header string and user headers[0] to be sure to only validate only the return code and message (example: 200, 404, 405, etc.)
Additional note 3: Sometime validating only the code 404 is not enough (see the unit test), so there's an optional $failCodeList parameter to supply all the code list to reject.
And, of course, here's the unit test (including all the popular social network) to legitimates my coding:
Great success to all,
Jonathan Parent-Lévesque from Montreal
to check if url is online or offline ---
get_headers() returns an array with the headers sent by the server in response to a HTTP request.
kind of an old thread, but.. i do this: