Jump to content

Using filesystem functions with HTTP error codes


ThePsion5

Recommended Posts

Hi,I'm trying to create a PHP script that basically tests for broken/inaccessable links. Is isn't enough, however to just know that they can't be accessed, which one can find out easily just by attempting to open a file with file_get_contents(). Is there anyway to retrieve the HTTP error code when a PHP function fails to open a remote file (without installing PEAR/PECL extensions)?

Link to comment
Share on other sites

Actually, I found an interesting way to record error information without using any of those (i'm not in a position to install PEAR/PECL extensions on this server). It's suprisingly easy:

@ob_flush();@ob_start();fopen(this/file/not/there.html, 'r');$Text = ob_get_contents();/*Warning:  fopen(this/file/not/there.html): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found.*/ob_end_clean();//use a regular expression to parse the error informationreturn $Parsed; //Something like "HTTP/1.0 404 Not Found"

Link to comment
Share on other sites

That's one way to do it, but when I simply want to test if something is present (like if a server is running) I use sockets. A socket just opens a connection. You can do other things with the socket if you want to, but it's not necessary. You can just close it again. One way to do this would be to use the various socket_ functions, like socket_create, socket_connect, etc, but there's a much easier way. You can just use fsockopen, which will also give you an error code and an error message if it fails. No output buffering or string parsing required.http://www.php.net/manual/en/function.fsockopen.php

<?php$port = 80;$fp = fsockopen("www.google.com", $port, $errno, $errstr);if (!$fp) {   echo "ERROR: $errno - $errstr<br />\n";} else {   fclose($fp);}?>

Here are the basics I use for a script to check on various servers, followed by some code to email my phone the results if there was a problem:

$errno = "";$errstr = "";$email_str = "";$server_list = array();$nr = 0;$server_list[$nr]['hostname'] = "server1.com";$server_list[$nr]['ip'] = "192.168.1.1";$server_list[$nr]['port'] = "21"; //ftp$nr++;$server_list[$nr]['hostname'] = "sub.server1.com";$server_list[$nr]['ip'] = "192.168.1.2";$server_list[$nr]['port'] = "25"; //smtp$nr++;$server_list[$nr]['hostname'] = "server1.com";$server_list[$nr]['ip'] = "192.168.1.1";$server_list[$nr]['port'] = "80"; //http$nr++;for ($i = 0; $i < count($server_list); $i++){  $conn = @fsockopen ($server_list[$i]['hostname'], $server_list[$i]['port'], $errno, $errstr, 30);  if (!$conn)	$email_str .= "{$server_list[$i]['hostname']} port {$server_list[$i]['port']}\n";  else	fclose($conn);  $conn = @fsockopen ($server_list[$i]['ip'], $server_list[$i]['port'], $errno, $errstr, 30);  if (!$conn)	$email_str .= "{$server_list[$i]['ip']} port {$server_list[$i]['port']}\n";  else	fclose($conn);}

Scripts that do this stuff will take a little time to execute though, because if the server is offline, it will sit there and wait a few seconds trying to connect. I specified the timeout there to be 30 seconds, which is fine for only 6 connections being tested (3 ports, via both hostname and IP). The email message is short because it's going to my cell phone and I don't want a 3 page message about a server being offline.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...