Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, April 28, 2010

How to get HTTP response code of HTTP request in PHP

Here the function to get the HTTP response code of a HTTP request. This function can easily be used to detect what is the status of a page that you are requesting (such as 404 - Not Found, 301 - Permenently Moved). Here is a complete reference for HTTP response codes.

  1. function getResponseCode($url) {
  2.     $ch= curl_init();
  3.  
  4.     curl_setopt($ch, CURLOPT_URL, $url);
  5.     curl_setopt($ch, CURLOPT_HEADER, 1);
  6.     curl_setopt($ch, CURLOPT_NOBODY, 1);
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8.     curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  9.  
  10.     curl_exec($ch);
  11.  
  12.     $details = curl_getinfo($ch);
  13.  
  14.     curl_close($ch);
  15.     return $details['http_code'];
  16. }

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.  
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.