php谷歌天气错误处理问题

php谷歌天气错误处理问题,php,Php,我有一个与google weather api交互的php脚本。一切正常,但如果我使用blahblah这样的错误位置进行测试,尽管我的脚本中有一些验证,但我在浏览器中还是会出现大量错误。php代码如下所示: <?php function googleWeather($location){ $url = "http://www.google.com/ig/api?weather=" . urlencode($location);

我有一个与google weather api交互的php脚本。一切正常,但如果我使用blahblah这样的错误位置进行测试,尽管我的脚本中有一些验证,但我在浏览器中还是会出现大量错误。php代码如下所示:

    <?php

        function googleWeather($location){

        $url = "http://www.google.com/ig/api?weather=" . urlencode($location); 
        $xml = simplexml_load_file($url); 

        $current_conditions = $xml->xpath("/xml_api_reply/weather/current_conditions");

        $temp_c = $current_conditions[0]->temp_c['data'];
        $temp_f = $current_conditions[0]->temp_f['data'];

        $result = sprintf("Current temperature is %s&deg; Celsius %s&deg;    Fahrenheit",$temp_c,$temp_f);

        return $result;



          }

        $loc = "blahblah";
        $goog = googleWeather($loc);

        if($goog == false){

          echo "An error occurred";

          }

          else{

          echo $goog;

               }

    ?>
( ! ) Notice: Undefined offset: 0 in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 11
Call Stack
#   Time    Memory  Function    Location
1   0.1086  370080  {main}( )   ..\TMP5c2a1z4emd.php:0
2   0.1086  370184  googleWeather( )    ..\TMP5c2a1z4emd.php:23

( ! ) Notice: Trying to get property of non-object in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 11
Call Stack
#   Time    Memory  Function    Location
1   0.1086  370080  {main}( )   ..\TMP5c2a1z4emd.php:0
2   0.1086  370184  googleWeather( )    ..\TMP5c2a1z4emd.php:23

( ! ) Notice: Undefined offset: 0 in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 12
Call Stack
#   Time    Memory  Function    Location
1   0.1086  370080  {main}( )   ..\TMP5c2a1z4emd.php:0
2   0.1086  370184  googleWeather( )    ..\TMP5c2a1z4emd.php:23

( ! ) Notice: Trying to get property of non-object in C:\wamp2\www\phpAcademy\GoogleWeatherApi\TMP5c2a1z4emd.php on line 12
Call Stack
#   Time    Memory  Function    Location
1   0.1086  370080  {main}( )   ..\TMP5c2a1z4emd.php:0
2   0.1086  370184  googleWeather( )    ..\TMP5c2a1z4emd.php:23
我用real city取代了blahblah,它开始工作了

可能您输入的城市地址语法错误。
尝试更改您的位置,例如苏黎世

对于无效的位置,Google将返回以下内容

<xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"><problem_cause data=""/></weather></xml_api_reply>

这些错误是不言自明的,$current_条件不包含您认为它的功能:

$current_conditions = $xml->xpath("/xml_api_reply/weather/current_conditions");

$temp_c = $current_conditions[0]->temp_c['data'];
//                           ^^^ undefined index                         
$temp_f = $current_conditions[0]->temp_f['data'];
//                              ^^^ Trying to access property of non object
您没有验证$current_条件的结果:

此外,您可能需要验证对象属性的存在,以及这些属性中是否存在数据键:

if (is_array($current_conditions) && isset($current_conditions[0]))
{
    $cur = $current_conditions[0];
    if (isset($cur->temp_c) && isset($cur->temp_c['data']))
    {
        $temp_c = $cur->temp_c['data'];
    }
    if (isset($cur->temp_f) && isset($cur->temp_f['data']))
    {
        $temp_f = $cur->temp_f['data'];
    }
}
这是相当冗长的,但绝对要避免这些通知-这是你必须做的。一旦应用程序上线,您可以而且应该关闭错误报告。通知旨在帮助您调试代码,如果您了解它们是如何生成的,并且可以安全地忽略它们,那么就可以了


而且,如果您确信API将为无效查询返回FALSE,只需检查$current_conditions===FALSE,您就可以跳过所有额外的验证

它的行是$temp_c=$current_conditions[0]->temp_c['data']$temp_f=$current_conditions[0]->temp_f['data'];,因为我不是谷歌API的专家,所以我真的帮不了你更多的忙。但是我想你不能把偏移量称为0;josnidhin,我遵循了你的代码,它是有效的。非常感谢你的帮助。
if (is_array($current_conditions) && isset($current_conditions[0]))
{
    $temp_c = $current_conditions[0]->temp_c['data'];
    $temp_f = $current_conditions[0]->temp_f['data'];
}
if (is_array($current_conditions) && isset($current_conditions[0]))
{
    $cur = $current_conditions[0];
    if (isset($cur->temp_c) && isset($cur->temp_c['data']))
    {
        $temp_c = $cur->temp_c['data'];
    }
    if (isset($cur->temp_f) && isset($cur->temp_f['data']))
    {
        $temp_f = $cur->temp_f['data'];
    }
}