simplexml加载谷歌天气api问题

时间:2011-04-07 14:00:54

标签: php simplexml

嗨我一直遇到谷歌天气api有错误的问题警告:simplexml_load_string()[function.simplexml-load-string]:实体:第2行:解析器错误....

我尝试使用主要作者的脚本(认为这是我编辑过的脚本),但我仍然遇到这个错误我试过2 //komunitasweb.com/2009/09/showing-the-weather-with-php-and-google-weather-api /

// tips4php.net/2010/07/local-weather-with-php-and-google-weather /

奇怪的部分有时会修复自己,然后又回到我已经使用它几个月的错误,没有任何问题,这只是昨天发生的。此外,作者的演示页面正在运行,但我有相同的确切代码,请帮助。

这是我的网站http://j2sdesign.com/weather/widgetlive1.php

@Mike我添加了你的代码

<?
$xml = file_get_contents('http://www.google.com/ig/api?weather=jakarta');  if (!     simplexml_load_string($xml)) {   file_put_contents('malformed.xml', $xml); }
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=jakarta');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");

&GT;

并列出了错误,但我似乎无法看到错误,因为它一直在修复自己然后又一次又回到了错误

这是文件的内容

<?php  include_once('simple_html_dom.php');  // create doctype $dom = new DOMDocument("1.0");  
// display document in browser as plain text 
// for readability purposes //header("Content-Type: text/plain");  
// create root element 
$xmlProducts = $dom->createElement("products"); 
$dom->appendChild($xmlProducts);  
$pages = array(     'http://myshop.com/small_houses.html',     'http://myshop.com/medium_houses.html',     'http://myshop.com/large_houses.html' )   foreach($pages as $page) {     $product = array();     $source = file_get_html($page);      foreach($source->find('img') as $src)     {         if (strpos($src->src,"http://myshop.com") === false)         {             $product['image'] = "http://myshop.com/$src->src";         }     }      foreach($source->find('p[class*=imAlign_left]') as $description)     {         $product['description'] =  $description->innertext;     }      foreach($source->find('span[class*=fc3]') as $title)     {         $product['title'] =  $title->innertext;     }      //debug perposes!      echo "Current Page: " . $page . "\n";     print_r($product);     echo "\n\n\n"; //Clear seperator } ?>

1 个答案:

答案 0 :(得分:2)

simplexml_load_string()失败时,您需要存储您尝试加载到某处以供审核的数据。检查数据是诊断导致错误的第一步。

$xml = file_get_contents('http://example.com/file.xml');

if (!simplexml_load_string($xml)) {
  file_put_contents('malformed.xml', $xml);
}