I'm attempting to write a script that pulls a list of URLs from my database, checks to see what the last modified date of that URL is, and updates the database with each URL's new last modified timestamp. It's working in most cases right now, but it seems fsockopen can't handle redirects very well according to some google searches.
This is especially problematic because most of the URLs I'm checking are RSS feeds and a lot of them redirect to feedburner now for analytics.
I'm hoping someone can help me deal with the redirect issue. Can someone help me through this? I'm not a developer, I've just hacked this together -- so, talk sllloooowwllllyyyy. Here's the script:
<?
$user = "user";
$password = "password";
$database = "database";
$host = "mysql.domain.com";
mysql_connect($host,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
function get_raw_header($host,$doc)
{
$httpheader = '';
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp)
{
echo $errstr.' ('.$errno.')';
}else{
fputs($fp, 'GET '.$doc.' HTTP/1.0'."\r\n".'Host: '.$host."\r\n\r\n");
while(!feof($fp))
{
$httpresult = fgets ($fp,1024);
$httpheader = $httpheader.$httpresult;
if (ereg("^\r\n",$httpresult))
break;
}
fclose ($fp);
}
return $httpheader;
}
function get_header_array($url)
{
$url = ereg_replace('http://','',$url);
$endHostPos = strpos($url,'/');
if(!$endHostPos) $endHostPos = strlen($url);
$host = substr($url,0,$endHostPos);
$doc = substr($url,$endHostPos,strlen($url)-$endHostPos);
if($doc == '') $doc = '/';
$raw = get_raw_header($host,$doc);
$tmpArray = explode("\n",$raw);
for ($i=0;$i<sizeof($tmpArray); $i++)
{
@list($name, $value) = explode(':', $tmpArray[$i], 2);
$array[trim($name)]=trim($value);
}
return $array;
}
//select every row in the database
$query = "SELECT * FROM database";
$result = mysql_query($query) or die(mysql_error());
//for each row in the database...
while($row = mysql_fetch_array($result)){
$remote_file = $row['url'];
$id = $row['id'];
$array = get_header_array($remote_file);
$timestamp = date('Y-m-d H:i:s',strtotime($array['Last-Modified']));
$insertquery = "UPDATE database SET timestamp = '$timestamp' WHERE id = '$id'";
$insert = mysql_query($insertquery) or die(mysql_error());
echo $row['name'] . ': ' . $timestamp . '<br>';
}
mysql_close();
?>