我为我的项目创建了一个加载 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'] ?>° 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 Weather 接收数据,因此结果为空。谁能告诉我该怎么做?