2011-03-01 72 views
0

我在运行cron作业时遇到问题。如何在cron作业中加载xml

这里是我的代码:

<?php 
    $doc = new DOMDocument(); 
    $doc->load('http://www.all-news.in/feed'); 

    $items = $doc->getElementsByTagName("item"); 

    mysql_connect('host','username','pass'); 
    mysql_select_db('dbname'); 

    $i=0; 
    foreach($items as $itemname) 
    { 
     // Here Business logic for Inserting the data 
    } 

    echo $i.' Imported'; 
?> 

当我运行在浏览器这个文件,它工作正常,但cron的执行时,我已经得到了以下错误:

<br /> 
<b>Warning</b>: DOMDocument::load(http://www.all-news.in/feed) [<a href='domdocument.load'>domdocument.load</a>]: failed to open stream: Connection timed out in <b>/home/content/98/7509598/html/amz/social-bookmark.in/cron_jobs/all_news.php</b> on line <b>4</b><br /> 
<br /> 
<b>Warning</b>: DOMDocument::load() [<a href='domdocument.load'>domdocument.load</a>]: I/O warning : failed to load external entity &quot;http://www.all-news.in/feed&quot; in <b>/home/content/98/7509598/html/amz/social-bookmark.in/cron_jobs/all_news.php</b> on line <b>4</b><br /> 
0 Imported 

回答

1

我想你代码出来,我没有得到任何错误。我修改了一点给我一些更多的反馈:

error_reporting(E_ALL); 
$doc = new DOMDocument(); 
$doc->load('http://www.all-news.in/feed'); 

$items = $doc->getElementsByTagName("item"); 
var_dump($items); 
// mysql_connect('host','username','pass'); 
// mysql_select_db('dbname'); 

$i=0; 
foreach($items as $itemname) 
{ 
    var_dump($itemname); $i++; 
    // Here Business logic for Inserting the data 
} 

echo $i.' Imported'; 

这给了我:

object(DOMNodeList)#2 (0) { } object(DOMElement)#3 (0) { } object(DOMElement)#5 (0) { } object(DOMElement)#3 (0) { } object(DOMElement)#5 (0) { } object(DOMElement)#3 (0) { } object(DOMElement)#5 (0) { } object(DOMElement)#3 (0) { } object(DOMElement)#5 (0) { } object(DOMElement)#3 (0) { } object(DOMElement)#5 (0) { } 10 Imported 

如果你在本地环境中测试(你的笔记本电脑可能),那么请尝试重新启动您的Web服务器。除此之外,我不确定什么是对不起。

+0

是的,谢谢,我已经整理出来了。问题出在主机帐户上。当我的cron作业运行时,他们处于维护模式。这就是为什么我有错误。非常感谢您的时间.. – Avinash 2011-03-04 13:48:34