非法的字符串偏移量';日期';


Illegal string offset 'date'

我正在尝试将朋友的Wordpress网站升级到Wordpress和PHP的最新版本。除了他在主页上使用的滚动新闻行情器错误地显示"非法字符串偏移‘日期’"外,一切都很好,没有显示任何新闻。这是脚本:

<?php
$xmlOption = get_option('xmlFeed');
if (!isset($xmlOption)) {
    $buildURL = "https://wordpress.org/news/feed/";
    $request  = curl_init();
    curl_setopt($request, CURLOPT_URL, $buildURL);
    curl_setopt($request, CURLOPT_HEADER, false);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($request);
    curl_close($request);
    $xml     = new SimpleXMLElement($result);
    $channel = $xml->channel;
    delete_option('xmlFeed');
    $otion = array(
        'xml' => $channel,
        'date' => date('y-m-d')
    );
    add_option('xmlFeed', $option);
}
if ($xmlOption['date'] == date('y-m-d')) {
    $channel = $xmlOption['xml'];
} else {
    $buildURL = "https://wordpress.org/news/feed/";
    $request  = curl_init();
    curl_setopt($request, CURLOPT_URL, $buildURL);
    curl_setopt($request, CURLOPT_HEADER, false);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($request);
    curl_close($request);
    $xml     = new SimpleXMLElement($result);
    $channel = $xml->channel;
    delete_option('xmlFeed');
    $otion = array(
        'xml' => $channel,
        'date' => date('y-m-d')
    );
    add_option('xmlFeed', $option);
}
$i = 0;
while ($i <= 5) {
    echo "<li><a href='" . $channel->item->$i->link . "' target='_blank'>" . $channel->item->$i->title . "</a></li>";
    $i++;
}
?>

我注意到$otion使用了两次,我认为这可能是一个拼写错误。但当我把它改为$option时,页面的其余部分没有被解析,所以我想这不是问题所在。

由于我不是一个程序员,我现在已经拔了两个晚上的头发。是时候在我一无所有之前寻求帮助了。有人能帮我做这个吗?

这不是我问题的真正答案,但我发现了另一个脚本,经过一些小的更改,它可以完美地工作。所以我很高兴。

<?php $rss = new DOMDocument(); $rss->load('http://wordpress.org/news/feed/'); $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']); $link = $feed[$x]['link']; echo '<li><a href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></li>'; } ?>

它更小更干净。感谢您的帮助@Marcus