2011-03-01 30 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