You are here

9 January 2017
valid response code 200

Check the HTTP Status Code of the website header using PHP. Check that url is giving valid response code of 2xx. You can modify this for different purposes. For detecting 404 not found only or separate error reporting for error 500.

Source code viewer
  1. function is_working_url($url) {
  2. $handle = curl_init($url);
  3. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  4. curl_setopt($handle, CURLOPT_NOBODY, true);
  5. curl_exec($handle);
  6.  
  7. $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  8. curl_close($handle);
  9.  
  10. if ($httpCode >= 200 && $httpCode < 300) {
  11. return true;
  12. }
  13. else {
  14. return false;
  15. }
  16. }
Programming Language: PHP