Tuesday 14 August 2018

How to check url exits or not in PHP

How to check file exist in given URL – PHP.
FOr check url exist or not you have to use below function for it.
you can also check for xml path exits or not.
 $url = "www.hariomrubber.com/sitemap.xml";
 
 if($this->is_url_exist($url))
 {
  echo "URL is exists";
 }else{
  echo "URL is not exists";
 }

 function is_url_exist($url)
 {
  $ch = curl_init($url);    
  curl_setopt($ch, CURLOPT_NOBODY, true);
  curl_exec($ch);
  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
  if($code == 200){
   $status = true;
  }else{
   $status = false;
  }
  curl_close($ch);
  return $status;
 }
 

0 comments:

Post a Comment