Cron problem Arrgggghhhh!

Hi All I am tryingto run the following script as a Cron job.

I get the following error.

It works if I point my browser at the script.

However if it runs as a cron I get the following error:

<br />
<b>Warning</b>:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: URL file-access is disabled in the server configuration in <b>/home/spanishp/public_html/currency.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>:  SimpleXMLElement::__construct(http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml) [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: failed to open stream: no suitable wrapper could be found in <b>/home/spanishp/public_html/currency.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>:  SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: I/O warning : failed to load external entity &quot;http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml&quot; in <b>/home/spanishp/public_html/currency.php</b> on line <b>9</b><br />
<br />
<b>Fatal error</b>:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/spanishp/public_html/currency.php:9
Stack trace:
#0 /home/spanishp/public_html/currency.php(9): SimpleXMLElement->__construct('http://www.ecb....', 0, true)
#1 {main}
 thrown in <b>/home/spanishp/public_html/currency.php</b> on line <b>9</b><br />
<?php

include "connect.inc.php";

$xml = new SimpleXMLElement(
    'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml',
    null,
    true
);

printf('XML received at %s <br />', $xml->Cube->Cube['time']);


$currency_date = $xml->Cube->Cube['time'];

$rates = array(
    'USD' => null,
	'DKK' => null,
	'GBP' => null,
	'SEK' => null,
	'NOK' => null,
	'RUB' => null,
	'AUD' => null
);

foreach($xml->Cube->Cube->Cube as $item){
    if(true === array_key_exists((string)$item['currency'], $rates)){
        $rates[(string)$item['currency']] = (float)$item['rate'];
    }
}

print_r($rates);
echo "<hr>".$rates['GBP']."<br>";

$USD = $rates['USD'];
$DKK = $rates['DKK'];
$GBP = $rates['GBP'];
$SEK = $rates['SEK'];
$NOK = $rates['NOK'];
$AUD = $rates['AUD'];
$RUB = $rates['RUB'];

$query = "INSERT INTO currency 
(currency_date, 
currency_USD,
currency_DKK,
currency_GBP,
currency_SEK,
currency_NOK,
currency_AUD,
currency_RUB
)values (
'$currency_date',
'$USD',
'$DKK',
'$GBP',
'$SEK',
'$NOK',
'$AUD',
'$RUB'
)";

echo "$query";
$result = mysql_query($query);

?>

Some systems have different config files for php, depending on if they’re running from the apache or cgi interface.

Try this from the command prompt to see if you have the right permission:


$ php -i | grep allow_url_fopen

As pmw57 says, it looks as if PHP does not have the right to go and fetch that file - for sensible reasons - you should not trust data from external sources.

There are ways round this, and one of the most popular ways is to use cURL.

IMO, You are making the problem harder to debug for yourself by trying to do 2 things at once.

a) fetch a file
b) extract some terms

So you should be prepared to write the file somewhere when you fetch it.

Task a) has quite a high chance of failing, so you have to be prepared to:

i) try and fetch it again (something cURL does)
ii) report this failure to you, is it a show stopper? Does your biz depend on it?
iii) fall back to using an old file (the previously fetched file)

THEN extract the terms.