Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect 401 error from fopen operation 2

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi there,

I have the following code and would like to modify it so that I only send an email if a 401 HTTP error is returned from the fopen operation. I've had a scan of the documentation but can't see how to pick up the error number.

Any help would be much appreciated!
Code:
  $testPage = fopen($testURL, 'r');
  if (!$testPage) {
    echo('<p>Unable to open URL</p>');
    $sent = @mail(MAIL_TO, MAIL_SUBJECT, MAIL_MESSAGE, 'From: ' . MAIL_FROM);
    if (!$sent) {
      exit('Mail could not be sent. Sorry!');
    }
  }
  else {
    fclose($testPage);
  }

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
You can't necessarily get that information using fopen(), as it doesn't provide the HTTP headers, only the content. If the web server in question doesn't provide a clue in the returned content (and even then usually with 401 after submitting bad credentials), you can't programmatically deduce the HTTP response code.

To get the granularity to see HTTP response codes, you're probably going to have to use fsockopen() or cURL functions.



Want the best answers? Ask the best questions! TANSTAAFL!
 
could you also use get_headers($url) (at least with php 5+)?
Code:
$url = "[URL unfurl="true"]http://www.google.co.uk";[/URL]
$headers = get_headers($url);
the response code should be in $headers[0];
 
Thanks for the tip sleipnir. In the end, I constructed the following code to check for a 401 error:
Code:
  $testURL = USER_SITE_URL;
  $authInfo = HTTP_USERNAME . ':' . $password;

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $testURL);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD, $authInfo);
  curl_setopt($ch, CURLOPT_FAILONERROR, True);
  $content = curl_exec($ch);
  curl_getinfo($ch, CURLINFO_HTTP_CODE);
  $errorMsg = curl_error($ch);
  $errorNumber = curl_errno($ch);

  // check if an HTTP error code has been returned
  if ($errorNumber == '22') {
    echo('<p>Unable to open URL</p>');
    // check if HTTP error code is a 401 unauthorized error
    if (substr_count($errorMsg, '401') > 0) {
      $sent = @mail(MAIL_TO, MAIL_SUBJECT, MAIL_MESSAGE, 'From: ' . MAIL_FROM);
  		if (!$sent) {
  			exit('Mail could not be sent. Sorry!');
  		}
    }
  }
  else {
    echo('<p>URL opened successfully</p>');
  }
  curl_close ($ch);

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Sorry jpadie, I don't think I refreshed the page before posting my answer. I will give your suggestion a try as an alternative to the cURL technique above.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
jpadie, initially when I used get_headers it returned a sensible value (i.e. some text containing "401") to the $headers variable and printed some warning text to the browser (see below). And I thought, this is a very neat and concise way to achieve my aim.

However, randomly it has suddenly decided it won't return anything into the $headers variable yet still prints the:
Code:
Warning: get_headers([URL unfurl="true"]http://...@www.blah.co.uk/pages/contacts.php)[/URL] [function.get-headers]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in ...

I googled it and apparently I'm not the only one to experience problems with the get_headers function:

So, I think I'll revert back to the cURL method. Thanks for the suggestion though jpadie, it would be a great way to do what I'm doing if a stable version were to be released.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
sorry to hear that. it looked like a great solution!

from tracking the material on the reported bugs, it appears that the issue was fixed in php 5.2 (
 
...and guess what? My host has 5.1.2 installed! Many thanks anyway jpadie. I'll store that technique away for the future!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top