Hi,
Have a problem with a php script to get the title of a URL. It works when I run it manually, but not when I run it through cron.
Googled to get a small script to get the title of a URL:
function getTitle($url) {
$fh = fopen($url, "r");
$str = fread($fh, 7500);
fclose($fh);
$str2 = strtolower($str);
$start = strpos($str2, "<title>")+7;
$len = strpos($str2, "</title>") - $start;
if ($start == 7)
return $url;
return substr($str, $start, $len);
}
I then run the below, where I look for urls in texts, and prints the url with titel:
$data = mysql_query('SELECT * FROM msgs ORDER BY id DESC LIMIT 100');
while ($rad = mysql_fetch_array($data)) {
preg_match_all($pattern, $rad["text"], $a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
echo 'URL:'.$a[1]["$row"].'<BR>';
echo 'TITLE:'.getTitle($a[1]["$row"]).'<BR><BR>';
}
}
The above code resides in url.php. When I run it manually through the browser it works fine and prints the url with correct title. However, when I run in as a scheduled cron job (once every minute) it writes url and url, i.e. it seems like getTitle always interpret "$start == 7" to be true.
Can this have to do with timing? Does fopen and fread take to much time? If so, how can I fix this.
I've seen a typical cron problem is env. variables, but I don't see how that can affect this?
Any help or ideas are welcome!