Wednesday, April 28, 2010

How to Validate RSS or ATOM feed using simple PHP Script

Here a simple function to Validate Feed URL,

  1. function getFeedStatus ($url) {
  2.     $ch = curl_init();
  3.     curl_setopt ($ch, CURLOPT_URL, $url);
  4.     curl_setopt ($ch, CURLOPT_HEADER, 0);
  5.     curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
  6.     curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  8.  
  9.     $string = curl_exec($ch);
  10.     curl_close($ch);
  11.  
  12.     if (strpos($string, '<feed') > 0 && strpos($string, '</feed')) {
  13.             return "ATOM";
  14.     } else if (strpos($string, '<channel') > 0 && strpos($string, '</channel')) {
  15.             return "RSS";
  16.     } else {
  17.             return "INV";
  18.     }
  19. }
  20.  

No comments:

Post a Comment

All rights reserved for all the posts. Please inform us on idhanu@gmail.com if you are publishing post in this without modifying. Code snippets are free to use, if not explicitly mentioned.