Cakephp 2.0:Google Weather API错误+ Xml
问题描述:
我为我的项目加载了Google Weather API创建了一个元素。它在本地完美运作。这个问题出现在我上传它的时候,以及当我把它交给我的一个朋友去尝试。Cakephp 2.0:Google Weather API错误+ Xml
该元素的代码是以下一个
<?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'] ?>° 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 ?>° C - <?= $high_celsius ?>° 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天气的数据,因此结果为空。任何人都可以告诉我该怎么办?
答
目前,您正在使用Google Weather API的间歇403 Forbidden响应。请参阅Google Weather API 403 Error
403响应可能会导致在结果的第一行中看到的空文档。现在再次在本地和服务器上重新运行代码,看看问题是否是间歇性的。
间歇403响应的原因尚不清楚,但是自2012年8月7日发布此问题以来一直存在问题。
这可能是!我检查过Chrome浏览器的“http://www.google.com/ig/api?weather=paris”,我收到了这条消息“403.这是一个错误。您的客户端没有获得URL/ig/api?weather =来自这台服务器的巴黎,这就是我们所知道的。“奇怪的是,它可以工作。在线版本不起作用。 – user1496047 2012-08-10 14:14:25
我可以在这里重现同样的事情。它始终从Chrome始终失败,从各种服务器情况中断断续续地发生,并且始终如一地或间歇地从Firefox进行工作。有可能403与谷歌某种类型的CDN /边缘变化有关,因为这可能导致间歇性行为。我们可以推测Google的基础设施处理/缓存/对待Chrome不同于Firefox。 – ClearCrescendo 2012-08-10 14:52:18
只做了一些测试,它不是ident字符串。这是头文件请求中的其他内容导致chrome失败的原因。 – ClearCrescendo 2012-08-10 15:40:31