tags:

views:

129

answers:

1

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!

+1  A: 

It usually happens because you have a different environment in your cron (or when you run as a different user).

When you run it in the command line you have a different environment that you can read by running "env".

You can change the environment prepending variables to the command you're running. Like:

PATH=/bin:/usr/local/bin php myprogram.php

Something else that you should do is enable error reporting in your PHP script.

fopen could return FALSE for instance and you have to catch such errors. "$start == 7" happens when you prepend the empty string to your expression thus this can be the result of an unhandled error.

Running error_reporting(E_ALL) while you debug could help here.

arhuaco