ChicagoDude
ChicagoDude

Reputation: 601

PHP cron job vs. access through web browser

I have a PHP script that is parsing an RSS feed and entering the info in a db. I am using PHP5.

It is a .php page, and works like a charm when accessed through the web server & browser. (http://myhost.com/rssjob.php)

Now i want to set it up as a CRON job - however when i execute it through SSH/CRON job - it throws exceptions like " Invalid argument supplied for foreach() " in following code.

Question: 1. Why is there a delta in the results i get through accessing the PHP page throw the browser and when i try to execute it as a CRON job/SSH command line?? Is there something more i should specify for the php page to work well when accessed through SSH/commandline/cron job?

$xml = parseRSS("http://www.myhost.com/rss/");

//SAMPLE USAGE OF 
foreach($xml['RSS']['CHANNEL']['ITEM'] as $item) {
        //echo("<p>");
    $title = $item['TITLE'];

    $description = $item['DESCRIPTION'];

Upvotes: 2

Views: 1470

Answers (2)

Alexei Tenitski
Alexei Tenitski

Reputation: 9370

A quick solution (hack) is to call your script via HTTP from cron:

? * * * * curl http://PATH.TO.YOUR/script.php

Upvotes: 2

julesj
julesj

Reputation: 754

There are a couple of differences in running a script through cron vs web. For example, cron will not have a $_SERVER['DOCUMENT_ROOT'] variable. If you are using that in your script, this will probably break it.

I'm not sure what happens before your sample lines, but for some reason, $xml is not the array that you expect it to be and like it appears in the web version. Please check what value the $xml variable has when running it from cron. An easy way to determine if the script is running from command-line / cron is:

$cron = (bool) defined('STDIN');

Upvotes: 0

Related Questions