我该如何处理这些PHP对象?

时间:2012-10-15 12:55:39

标签: php object

我正在使用Google地图API V3向lat / lng发出请求,期望获得返回的完整地址数据。但是,我的PHP程序无法获取,例如,有时会返回Zip代码。我做了一个关于返回内容的var_dump。我看到一个额外的对象元素,这可能是我不确定的原因。我完全有可能不了解PHP中的Objects如何工作。这是感兴趣区域的var_dump输出。第一个例子确实将Zip Code返回到我的PHP程序发出请求,而第二个例子没有这样做,并给出错误“试图获取非对象的属性......”。提前感谢您查看此内容以及您的有用评论。

这是从Google Map API V3返回的$ xml的var_dump,它不起作用并且无法正确返回邮政编码:

object(SimpleXMLElement)#1 (1) {
  ["Response"]=>
  object(SimpleXMLElement)#2 (3) {
    ["name"]=>
    string(24) "40.74005999,-73.99718229"
    ["Status"]=>
    object(SimpleXMLElement)#3 (2) {
      ["code"]=>
      string(3) "200"
      ["request"]=>
      string(7) "geocode"
    }
    ["Placemark"]=>
    object(SimpleXMLElement)#4 (5) {
      ["@attributes"]=>
      array(1) {
        ["id"]=>
        string(2) "p1"
      }
      ["address"]=>
      string(38) "136 W 17th St, New York, NY 10011, USA"
      ["AddressDetails"]=>
      object(SimpleXMLElement)#5 (2) {
        ["@attributes"]=>
        array(1) {
          ["Accuracy"]=>
          string(1) "8"
        }
        ["Country"]=>
        object(SimpleXMLElement)#8 (3) {
          ["CountryNameCode"]=>
          string(2) "US"
          ["CountryName"]=>
          string(3) "USA"
          ["AdministrativeArea"]=>
          object(SimpleXMLElement)#9 (2) {
            ["AdministrativeAreaName"]=>
            string(2) "NY"
            ["SubAdministrativeArea"]=>
            object(SimpleXMLElement)#10 (2) {
              ["SubAdministrativeAreaName"]=>
              string(8) "New York"
              ["Locality"]=>
              object(SimpleXMLElement)#11 (2) {
                ["LocalityName"]=>
                string(8) "New York"
                ["DependentLocality"]=>
                object(SimpleXMLElement)#12 (3) {
                  ["DependentLocalityName"]=>
                  string(9) "Manhattan"
                  ["Thoroughfare"]=>
                  object(SimpleXMLElement)#13 (1) {
                    ["ThoroughfareName"]=>
                    string(13) "136 W 17th St"
                  }
                  ["PostalCode"]=>
                  object(SimpleXMLElement)#14 (1) {
                    ["PostalCodeNumber"]=>
                    string(5) "10011"
                  }
                }
              }
            }
          }
        }
      }
      ["ExtendedData"]=>
      object(SimpleXMLElement)#6 (1) {
        ["LatLonBox"]=>
        object(SimpleXMLElement)#8 (1) {
          ["@attributes"]=>
          array(4) {
            ["north"]=>
            string(10) "40.7414089"
            ["south"]=>
            string(10) "40.7387109"
            ["east"]=>
            string(11) "-73.9958332"
            ["west"]=>
            string(11) "-73.9985312"
          }
        }
      }
      ["Point"]=>
      object(SimpleXMLElement)#7 (1) {
        ["coordinates"]=>
        string(24) "-73.9971822,40.7400599,0"
      }
    }
  }
}

上面有这些错误:

PHP Notice:  Trying to get property of non-object in try.php on line 38
PHP Notice:  Trying to get property of non-object in try.php on line 41

以下是这些行的PHP代码:

$status = $xml->Response->Status->code;
// Country
$country = $xml->Response->Placemark->AddressDetails->Country;
$country_code = $country->CountryNameCode;
$country_name = $country->CountryName;

// Address
$address_line = $xml->Response->Placemark->address;

// Street address
$Locality = $country->AdministrativeArea->Locality;
// var_dump($Locality);
$street = $country->AdministrativeArea->Locality->Thoroughfare->ThoroughfareName;
$city = $country->AdministrativeArea->Locality->LocalityName;
$state = $country->AdministrativeArea->AdministrativeAreaName;
$zip_code = $country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;

第41行是$ zip_code的最后一行代码,第38行是以$ street开头的代码行。

您会注意到上面包含[“SubAdministrativeArea”] =>下面的工作示例没有。这有关系吗?

object(SimpleXMLElement)#1 (1) {
  ["Response"]=>
  object(SimpleXMLElement)#2 (3) {
    ["name"]=>
    string(24) "40.74445606,-73.97495072"
    ["Status"]=>
    object(SimpleXMLElement)#3 (2) {
      ["code"]=>
      string(3) "200"
      ["request"]=>
      string(7) "geocode"
    }
    ["Placemark"]=>
    object(SimpleXMLElement)#4 (5) {
      ["@attributes"]=>
      array(1) {
        ["id"]=>
        string(2) "p1"
      }
      ["address"]=>
      string(38) "317 E 34th St, New York, NY 10016, USA"
      ["AddressDetails"]=>
      object(SimpleXMLElement)#5 (2) {
        ["@attributes"]=>
        array(1) {
          ["Accuracy"]=>
          string(1) "8"
        }
        ["Country"]=>
        object(SimpleXMLElement)#8 (3) {
          ["CountryNameCode"]=>
          string(2) "US"
          ["CountryName"]=>
          string(3) "USA"
          ["AdministrativeArea"]=>
          object(SimpleXMLElement)#9 (2) {
            ["AdministrativeAreaName"]=>
            string(2) "NY"
            ["Locality"]=>
            object(SimpleXMLElement)#10 (3) {
              ["LocalityName"]=>
              string(8) "New York"
              ["Thoroughfare"]=>
              object(SimpleXMLElement)#11 (1) {
                ["ThoroughfareName"]=>
                string(13) "317 E 34th St"
              }
              ["PostalCode"]=>
              object(SimpleXMLElement)#12 (1) {
                ["PostalCodeNumber"]=>
                string(5) "10016"
              }
            }
          }
        }
      }
      ["ExtendedData"]=>
      object(SimpleXMLElement)#6 (1) {
        ["LatLonBox"]=>
        object(SimpleXMLElement)#8 (1) {
          ["@attributes"]=>
          array(4) {
            ["north"]=>
            string(10) "40.7458050"
            ["south"]=>
            string(10) "40.7431070"
            ["east"]=>
            string(11) "-73.9736017"
            ["west"]=>
            string(11) "-73.9762997"
          }
        }
      }
      ["Point"]=>
      object(SimpleXMLElement)#7 (1) {
        ["coordinates"]=>
        string(24) "-73.9749507,40.7444560,0"
      }
    }
  }
}

以下是用于输出的代码行,其中$ zip_code在第一个示例中为空白,而第二个示例则按预期工作。

echo "===================================\n";
echo "Status code is: " . $status . "\n";
echo "address line is:" . $address_line . "\n";
echo "=+=+=" . "\n";
echo "street is: " . $street . "\n";
echo "city is: " . $city . "\n";
echo "state is: " . $state . "\n";
echo "zip code is: " . $zip_code . "\n";
echo "country code is: " . $country_code . "\n";
echo "country name is: " . $country_name. "\n";

此示例中使用的不同位置均位于纽约市。我不知道为什么一个工作而另一个不工作,我希望我看到它正在返回[“SubAdministrativeArea”]。 Google地图API V3不应该为同一区域返回相同格式的信息吗?这甚至是我遇到的问题的一个因素吗?我没有正确处理对象和元素吗?如果是这样,请赐教,因为我现在陷入困境。

或者我应该检查Google地图API V3可能返回的两种不同情况(或更多?)?谢谢!

1 个答案:

答案 0 :(得分:2)

  

您会注意到上面包含[“SubAdministrativeArea”] =>下面的工作示例没有。这有关系吗?

是的,这很重要。

  

Google地图API V3不应该为同一区域返回相同格式的信息吗?

答案的某些元素将始终存在于结果树中的相同位置,但其他一些元素依赖于请求。我无法帮助你,因为我永远不明白为什么即使请求非常相似,有时会有更多的分层......

  

这甚至是我遇到的问题的一个因素吗?

是的,因为你不能依赖固定的结果结构。

  

我应该检查Google地图API V3可能返回的两种不同情况(或更多吗?)?

另一种解决方案可以是使用除SimpleXML之外的其他库来处理结果,例如DOM,其具有通过树查找子项的功能,如DOMElement::getElementsByTagName。获取值需要比使用SimpleXML更多的编码,但这样您无需检查是否存在SubAdministrativeArea级别。

我希望它有所帮助。