user5214430
user5214430

Reputation:

PHP works in browser but not cron

I need to help here because I am going mad! I have a PHP script which as far as I can tell was working when it was made, in both browser and via cron/cli. However this has recently stopped working for some reason, and after investigation it is returning a "Undefined variable" error :(

So, I have viewedme.php, which called checklogin.php, which in turn calls functions.inc.php.

When run directly from the browser the script executes from end to end and performs all the actions it was designed for. However when I run this in cron using "/usr/bin/php /bla/cron_updateviewed.php", it returns this error message...

PHP Notice: Undefined variable: match in /bla/bla/functions.inc.php on line 79

What is even more annoying for me, is I have many other scripts, all of which are run from cron, calling the same function in the same manner, without error.

This is my viewedme.php...

<?php
include_once('checklogin.inc.php');
include_once('mysqli.inc.php');

$ch = curl_init();

// more curl settings here but removed as not important

$html = striplinks($html);
?>

checklogin.inc.php calls functions.inc.php, which includes this function...

// Function to strip down to links
function striplinks($document) {
    preg_match_all("'<\s*a\s.*?href\s*=\s*          # find <a href=
        ([\"\'])?                   # find single or double quote
        (?(1) (.*?)\\1 | ([^\s\>]+))        # if quote found, match up to next matching
        # quote, otherwise match up to next space
        'isx", $document, $links);

    while (list($key, $val) = each($links[2])) {
        if (!empty($val))
            $match[] = $val;
    }

    while (list($key, $val) = each($links[3])) {
        if (!empty($val))
            $match[] = $val;
    }

    return $match;
}

Like I said, Viewed.php is all working when access from the browser, but just not cron. Also important to add there is nothing being passed through the browser like POST or GETs which would stop this from working.

I have other scripts such as checkinbox.php which uses the striplinks function fron cron without any issues.

I am really at a loss as to wtf is going on :(

Updated

Solved "undefined variable" issue, but still returns nill

As suggested the regex wasn't being executed so I have added $match = array(); into the function before the while loops. THis fixes the undefined variable error, but it still returns 0.

After setting up a few test scripts I have found when executing the code though the browser, the cookies in curl are used. However when executing through cron/cli they are not used, therefore a different webpage is being displayed.

Here is my curl code...

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie');
curl_setopt($ch, CURLOPT_URL, 'http://www.bla.com/');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);

Is anything wrong with it as to why cookies are not used when executing via cron/cli?

Upvotes: 2

Views: 938

Answers (2)

user5214430
user5214430

Reputation:

So after over 4 hours of trial, error, and anger I finally solved the problem. It appears CURL requires the full path to the cookie files if you are going to execute from cli or cron. Strange but there you go :P

curl_setopt($ch, CURLOPT_COOKIEJAR, '/home/full/path/cookie.txt');

Thanks to @Siguza for the array suggestion too.

Upvotes: 2

Siguza
Siguza

Reputation: 23850

My bet is that both your while loops or the ifs they contain never run, and $match stays undefined, causing the error on return.
You can fix this by initializing $match to an empty array like so:

function striplinks($document) {
    $match = array(); // also possible for PHP5.4 and later: $match = [];

Upvotes: 2

Related Questions