HTTP error codes with fopen()?

Discussion in 'PHP' started by scx84, Jun 11, 2006.

  1. #1
    Hi
    Is there a way of retrieving HTTP error codes (404, 401, ...) when opening an url with fopen()?
    This function seems to either return a handle (success) or just false (failure).

    Cheers!
     
    scx84, Jun 11, 2006 IP
  2. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
      $url = 'use your URL here';  
      $fp = fopen($url, 'r');
      
      // The variable $http_response_header is now automagically populated
      print_r($http_response_header);
    ?>
    
    PHP:
    Enjoy!

    Bobby
     
    Chemo, Jun 11, 2006 IP
  3. scx84

    scx84 Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a lot Chemo, exactly what I was looking for! :D

    For anyone interested, the full array of $http_response_header looks something like:

    
    [0] => HTTP/1.1 401 Authorization Required
    [1] => Date: Sun, 11 Jun 2006 17:18:33 GMT
    [2] => Server: Apache/1.3.29 (Unix) PHP/4.4.0
    [3] => WWW-Authenticate: Basic realm="Members"
    [4] => Connection: close
    [5] => Content-Type: text/html; charset=iso-8859-1
    
    Code (markup):
    To check the returned HTTP (error) code, do something like:
    
    if (strpos($http_response_header[0], '404') !== false)
         echo "Error: Document not found.";
    
    PHP:
     
    scx84, Jun 11, 2006 IP
  4. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You could expand that and use something like this:
    
    	$return_code = @explode(' ', $http_response_header[0]);
    	$return_code = (int)$return_code[1];
    	 
    	switch($return_code){
    		case 301:
    			// do something
    			break;
    		case 302:
    			// do something
    			break;
    		case 404:
    			// do something
    			break;
    		default:
    			// do something
    			break;
    	}
    
    PHP:
    Bobby
     
    Chemo, Jun 11, 2006 IP
  5. scx84

    scx84 Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Looks like an elegant solution :p
    Is the return code always of the format "HTTPsomething Number Message"?
     
    scx84, Jun 11, 2006 IP
  6. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes...if it is a 301 or 302 there will be an additional element in the array of the form Location: domain.com/new_url.php. I have that in my switch statement to check the number of redirects and also present the final URL.
     
    Chemo, Jun 11, 2006 IP
  7. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Huh this doesn't work for me eg
    
    $content = file_get_contents($url);
    var_dump($http_response_header);  //issues a notice $http_response_header not defined
    
    //while this one works fine
    var_dump(get_headers($url) );
    
    PHP:
    this is on PHP 5.1.4 MacOSX
     
    classic, Oct 23, 2008 IP
  8. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So is this an alternative to specifying an ErrorDocument in .htaccess?
     
    Omzy, Oct 23, 2008 IP
  9. salahsoftware

    salahsoftware Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i think so, this is a good alternative.
     
    salahsoftware, Oct 23, 2008 IP