0

我有一个 PHP 错误:

致命错误:在第 37 行的 /weather /classes/BxWeatherModule.php 中的非对象上调用成员函数 getElementsByTagName ()

这是代码:

function serviceWeatherIndexPage() {
    include("geoipcity.inc");
    include("geoipregionvars.php");
    $ip = $_SERVER['REMOTE_ADDR'];
    $weather_feed = "";
    $pathr= BX_DOL_URL_ROOT;
    $gi = geoip_open("../GeoLiteCity.dat",GEOIP_STANDARD);
    $record = geoip_record_by_addr($gi,$ip);
    geoip_close($gi);
    $city = $record->city;
    if ($city == "") 
        $city = "Sydney";
    $url_post = "http://where.yahooapis.com/v1/places.q('".urlencode($city)."')?appid=foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-";
    $weather_feed = file_get_contents($url_post);
    $objDOM = new DOMDocument();
    $objDOM->loadXML($weather_feed);
    $woeid = $objDOM->getElementsByTagName("place")->item(0)->getElementsByTagName("woeid")->item(0)->nodeValue;
}
4

2 回答 2

0

因为您未能使用file_get_contents()加载数据,所以DOM结构没有任何内容,因此出现错误。要解决它,您需要在请求其属性等之前检查 ELEMENT是否为对象

$url_post = "http://where.yahooapis.com/v1/places.q(".var_dump(urlencode($city)).")?appid=foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-";
$weather_feed = file_get_contents($url_post);
$objDOM = new DOMDocument();
$objDOM->loadXML($weather_feed);
if (is_object($objDOM->getElementsByTagName("place")->item(0))){
    $woeid = $objDOM->getElementsByTagName("place")->item(0)->getElementsByTagName("woeid")->item(0)->nodeValue; 
 }
于 2015-06-01T20:24:19.463 回答
-1

这对我有用,试试这个:

<?php

    $sUrl = "http://where.yahooapis.com/v1/places.q('paris')?appid=foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-";
    $sXml = file_get_contents($sUrl);

    $oXml = new DOMDocument();
    $oXml->loadXML($sXml);

    try {
        $sWoeid = $oXml
            ->getElementsByTagName('place')->item(0)
            ->getElementsByTagName('woeid')->item(0)
            ->nodeValue;

    } catch (Exception $oException) {
        print 'Malformed XML';
    }

    print "WOEID is $sWoeid";

?>

于 2013-06-29T22:01:22.703 回答