0

我在运行 cron 作业时遇到问题。

这是我的代码:

<?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
4

1 回答 1

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 服务器。除此之外,我不确定出了什么问题对不起。

于 2011-03-01T18:05:26.030 回答