【问题标题】:Cakephp 2.0: Errors with Google Weather API + XmlCakephp 2.0:Google Weather API + Xml 的错误
【发布时间】:2012-08-07 08:45:00
【问题描述】:

我为我的项目创建了一个加载 Google Weather API 的元素。在本地它工作得很好。当我上传它,当我把它交给我的朋友尝试时,问题就出现了。

元素的代码如下

<?php
Configure::write('debug', 2);

$xml = simplexml_load_file('http://www.google.com/ig/api?weather=name_city');
$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");

?>
<html>
<head>
<title>Google Weather API</title>
</head>
<body>
<h1><?= print $information[0]->city['data']; ?></h1>
<h2>Today's weather</h2>
<div class="weather">
<?php $icon_today = (string) $current[0]->condition['data'];?>

<?php echo $this->Html->image('tick/'.$icon_today.'.png');?>
        <span class="condition">
        <?= $current[0]->temp_c['data'] ?>&deg; C,

        </span>
 </div>
<h2>Forecast</h2>
    <? foreach ($forecast_list as $forecast) : ?>
    <div class="weather">
    <?php $icon = (string) $forecast->condition['data'];?>
      <?php echo $this->Html->image('tick/'.$icon.'.png',array('width' => '45'));?>

        <div><?= $forecast->day_of_week['data']; ?></div>
        <span class="condition">
        <?php
        $low = (int) $forecast->low['data'];
        $high = (int) $forecast->high['data'];
        $low_celsius = (int) (($low - 32) * (5/9));
        $high_celsius = (int)(($high - 32) * (5/9));
        ?>
            <?= $low_celsius ?>&deg; C - <?= $high_celsius ?>&deg; C,
            <?= $forecast->condition['data'] ?>
        </span>
 </div>
<? endforeach ?>
</body>
</html>

当我上传它时,我收到了这些消息

Warning (2): simplexml_load_file() [function.simplexml-load-file]:     
http://www.google.com/ig/api?weather=name_city: parser error : Document is empty     
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]:   
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]: ^ 
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]: 
http://www.google.com/ig/api?weather=name_city:1: parser error : Start tag 
expected, '<' not found [APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]:  
[APP/View/Elements/weather.ctp, line 5]
Warning (2): simplexml_load_file() [function.simplexml-load-file]: ^ 
[APP/View/Elements/weather.ctp, line 5]
Fatal error: Call to a member function xpath() on a non-object in 
/home/wwwsite/public_html/testing/app/View/Elements/weather.ctp on line 6

name_city 具有实际价值。当我在另一台计算机上尝试时,其他类型的错误(例如,$forecast->day_of_week['data'] 未被识别为有效变量)。似乎在线版本无法从 Google Weather 接收数据,因此结果为空。谁能告诉我该怎么做?

【问题讨论】:

    标签: cakephp-2.0 google-weather-api


    【解决方案1】:

    Google 计划关闭 iGoogle 和相关 API。从 8 月 27 日起已经stopped responding weather API。你可以使用wunderground.com的api

    【讨论】:

      【解决方案2】:

      目前对您使用的 Google Weather API 有间歇性的 403 Forbidden 响应。见Google Weather API 403 Error

      403 响应可能导致您在结果的第一行中看到空文档。现在在本地和服务器上再次重新运行代码,看看问题是否是间歇性的。

      间歇性 403 响应的原因尚不清楚,但自 2012 年 8 月 7 日(即您发布此问题的时间)以来一直存在问题。

      【讨论】:

      • 可能就是这样!我已经用 Chrome 检查了“google.com/ig/api?weather=paris”并收到了这条消息“403。这是一个错误。您的客户端无权从该服务器获取 URL /ig/api?weather=paris。这就是我们所知道的。 "奇怪的是,它与 Firefox 一起工作。在线版本不起作用。
      • 我可以在这里重现同样的事情。它在 Chrome 中始终失败,在各种服务器情况下间歇性地失败,并且在 Firefox 中始终或间歇性地工作。 403 可能与 Google 的某种类型的 CDN/边缘更改有关,因为这可能导致间歇性行为。我们可以推测,谷歌的基础设施处理/缓存/处理 Chrome 与 Firefox 不同。
      • 刚刚做了一些测试,它不是 ident 字符串。标头请求中的其他内容会导致 chrome 持续失败。
      猜你喜欢
      • 1970-01-01
      • 2011-12-31
      • 2012-08-21
      • 2018-02-04
      • 2020-11-08
      • 2012-01-17
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多