2013-06-29 4 views
0

У меня есть ошибка PHP:getElementsByTagName() на не-объект

Fatal error: Call to a member function getElementsByTagName() on a non-object in /weather /classes/BxWeatherModule.php on line 37

Вот код:

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; 
} 

ответ

-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"; 

?>

0

да, но я хочу приложение с этим кодом:

$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); 
$woeid = $objDOM->getElementsByTagName("place")->item(0)->getElementsByTagName("woeid")->item(0)->nodeValue; 

У меня есть другая ошибка: строку (20) "Garges-л% E8s-Гонесса" Внимание: file_get_contents (http://where.yahooapis.com/v1/places.q()?appid=foOF4CzV34EFIIW4gz1lx0Ze1em._w1An3QyivRalpXCK9sIXT5de810JWold3ApkdMdCrc-) [function.file-Get-содержание]: не удалось открыть поток: HTTP, запрос не выполнен! HTTP/1.0 400 Bad Request в /homez.627/dolphint/www/Dolphin-Boonex-Templates/modules/doldesign/newsinfos/weather/index.php на линии 19

Предупреждение: DOMDocument :: loadXML() [DOMDocument .loadxml]: пустая строка, поставляемая в качестве входа в /homez.627/dolphint/www/Dolphin-Boonex-Templates/modules/doldesign/newsinfos/weather/index.php в строке 21

Неустранимая ошибка: позвоните участнику function getElementsByTagName() на не-объекте в /homez.627/dolphint/www/Dolphin-Boonex-Templates/modules/doldesign/newsinfos/weather/index.php в строке 22

0

Поскольку вы не смогли загрузить данные с file_get_contents() t он 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; 
} 
Смежные вопросы