PHP致命错误:在Google Calendar XML的非对象上调用成员函数asXML()

时间:2014-12-02 20:16:03

标签: php web

我需要一些关于此代码的帮助。它在几个星期前完美运行。

<?php
date_default_timezone_set('America/New_York');
$cache_time = 3600*12;

$feedRegular = "https://www.google.com/calendar/feeds/invernesscalvary.com_fipcro22aiul8uaa2hg80h6gmc%40group.calendar.google.com/public/basic?orderby=starttime&singleevents=true&futureevents=true&max-results=999&sortorder=a";
$feedFeature = "https://www.google.com/calendar/feeds/invernesscalvary.com_8ohrv5fhqf7o2r9jhgsr2q2764%40group.calendar.google.com/public/basic?orderby=starttime&singleevents=true&futureevents=true&max-results=3&sortorder=a";

$cache_file_reg = $_SERVER['DOCUMENT_ROOT'].'/cache/gcal.xml';
$timedif_reg = @(time() - filemtime($cache_file_reg));
$xmlRegular = "";

if (file_exists($cache_file_reg) && $timedif_reg < $cache_time) {
    $str_reg = file_get_contents($cache_file_reg);
    $xmlRegular = simplexml_load_string($str_reg);
    } else {
    $xmlRegular = simplexml_load_file($feedRegular);
    if ($f_reg = fopen($cache_file_reg, 'w')) {
        $str_reg = $xmlRegular->asXML();
        fwrite ($f_reg, $str_reg, strlen($str_reg));
        fclose($f_reg);
    } else { echo "<p>Can't write to the cache.</p>"; }
}

$xmlRegular->asXML();

$cache_file_fea = $_SERVER['DOCUMENT_ROOT'].'/cache/gcal_featured.xml';
$timedif_fea = @(time() - filemtime($cache_file_fea));
$xmlFeature = "";

if (file_exists($cache_file_fea) && $timedif_fea < $cache_time) {
    $str_fea = file_get_contents($cache_file_fea);
    $xmlFeature = simplexml_load_string($str_fea);
} else {
    $xmlFeature = simplexml_load_file($feedFeature);
    if ($f_fea = fopen($cache_file_fea, 'w')) {
        $str_fea = $xmlFeature->asXML();
        fwrite ($f_fea, $str_fea, strlen($str_fea));
        fclose($f_fea);
    } else { echo "<p>Can't write to the cache.</p>"; }
}

if (is_object($xmlFeature)) {
    $xmlFeature->asXML();
}

$n = 0;

foreach ($xmlRegular->entry as $entry) {
    $n++;
    $ns_gd = $entry->children('http://schemas.google.com/g/2005');

    $eDate = date("M", strtotime($ns_gd->when->attributes()->startTime));
    $eDay = date("j", strtotime($ns_gd->when->attributes()->startTime));
    $eTime = date("g:ia", strtotime($ns_gd->when->attributes()->startTime));

    $eLink = date("Y/m", strtotime($ns_gd->when->attributes()->startTime));

    $title = str_replace(" & ", " &amp; ", $entry->title);
    $where = $ns_gd->where->attributes()->valueString;

                        $link = $entry->link->attributes()->href;
    echo "<li>\n";
    echo "<span class=\"eDate\">$eDate <span class=\"eDay\">$eDay</span> @ $eTime</span><a rel=\"$eLink\" title=\"@ $where\" href=\"#\">";
    echo "<p class=\"eTitle\">$title</p></a>\n";
    echo "</li>\n\n";
    if($n == 3) break;
}

?>

FIXED 致命错误:在第83行的/home/calvary/public_html/index.php中的非对象上调用成员函数asXML()

FIXED 如果我在simplexml_load_string($ str_reg)之后执行var_dump($ xmlRegular),我会得到一个布尔值false。

更新 Warning: main() [function.main]: Node no longer exists in /home/calvary/public_html/index.php.new on line 132现在每行显示两次132,133,134,136和139,这些行以$ eDate $ eDay $ eTime $ eLink和$ where开头

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为$ xmlRegular变量不是对象。可能,它是一个带有false值的布尔值。这是因为simplexml_load_string或simplexml_load_file无法返回对象。我相信最可能的原因是simplexml_load_file没有读取权限或无法找到该文件。

我建议var_dump一个值,然后运行'exit;'看看那时的价值是多少。这样您就可以看到哪个值不正确。

如果您在此方面取得任何进展,请告诉我们,我们很乐意提供更多意见!

编辑:直接转到您正在使用的网址($ feedRegular)我收到了Forbidden 403错误。这会导致$ xmlRegular为false而不是对象。