2011-08-12 13 views
5

Ben Geocoding için birden webservices (yani Google, Yahoo, Bing, vb) kullanabileceği bir Geocoding sınıfını inşa ediyorum iç içe. Ben böyle yeni webservices kolayca konfigüre edilebilir bir yol yapmaya çalışıyorum. webservices çoğu benim birincil odak noktası olarak XML seçti PHP .. Ya XML/JSON dönmek. Tüm kod yer zaten, ama mesela artık Google (a simple_xml_element şekilde değiştirilmiş) aşağıdaki XMLDinamik erişim nesnesi

SimpleXMLElement Object 
(
[status] => OK 
[result] => Array 
    (
     [0] => SimpleXMLElement Object 
      (
       [type] => postal_code 
       [formatted_address] => 1010 Lausanne, Switzerland 
       [address_component] => Array 
        (
         [0] => SimpleXMLElement Object 
          (
           [long_name] => 1010 
           [short_name] => 1010 
           [type] => postal_code 
          ) 

         [1] => SimpleXMLElement Object 
          (
           [long_name] => Lausanne 
           [short_name] => Lausanne 
           [type] => Array 
            (
             [0] => locality 
             [1] => political 
            ) 

          ) 

         [2] => SimpleXMLElement Object 
          (
           [long_name] => Vaud 
           [short_name] => VD 
           [type] => Array 
            (
             [0] => administrative_area_level_1 
             [1] => political 
            ) 

          ) 

         [3] => SimpleXMLElement Object 
          (
           [long_name] => Switzerland 
           [short_name] => CH 
           [type] => Array 
            (
             [0] => country 
             [1] => political 
            ) 

          ) 

        ) 

       [geometry] => SimpleXMLElement Object 
        (
         [location] => SimpleXMLElement Object 
          (
           [lat] => 46.5376186 
           [lng] => 6.6539665 
          ) 

         [location_type] => APPROXIMATE 
         [viewport] => SimpleXMLElement Object 
          (
           [southwest] => SimpleXMLElement Object 
            (
             [lat] => 46.5253574 
             [lng] => 6.6384420 
            ) 

           [northeast] => SimpleXMLElement Object 
            (
             [lat] => 46.5467887 
             [lng] => 6.6745222 
            ) 

          ) 

         [bounds] => SimpleXMLElement Object 
          (
           [southwest] => SimpleXMLElement Object 
            (
             [lat] => 46.5253574 
             [lng] => 6.6384420 
            ) 

           [northeast] => SimpleXMLElement Object 
            (
             [lat] => 46.5467887 
             [lng] => 6.6745222 
            ) 

          ) 

        ) 

      ) 
) 

ihtiyacım bilgiler [konum] etiketinde olduğu döndürür, bu yüzden yolunu depolamak denedim bir var içinde:

(suppose $xml is the object) 
$xml->{$lat_path}; 

Ama bu işi doens't:

$lat_path = 'result[0]->geometry->location->lat; 

Ve sonra değerini bu şekilde erişmeyi deneyin. Bilgiye dinamik veya değişken tabanlı olarak erişebilmemin herhangi bir yolu var mı? Google özel kodu ile benim Coğrafi Kodlama yöntemi mahvetmek istemiyoruz.

Teşekkürler!

+0

. (eğer xml'den bazılarını gösterebilirseniz, örnek bir uygulama sağlamaya çalışacağım) – Yoshi

+0

Bunun için bir yöntem yazabilir ve unutabilirsiniz .. – Vytautas

+0

Ayrıca Xpath kullanmayı denedim, ama işe yaramadı :( print_r ($ xml> XPath ('geometri/Yer')); bana boş bir dizi vermiştir. –

cevap

2

Eğer

$xml->{$lat_path}; 

PHP değişken adı olarak $lat_path içinde herhangi kullanacaktır yaptığınızda. Bu değil nesne grafiğinde gitmek veya hiç T_OBJECT_OPERATOR uyacağız. Sadece $xml numaralı telefondan

'a bakacaktır. Örnek için bu kodu çalıştırmayı deneyin:

$obj = new StdClass; 
$obj->{'result[0]->geometry->location->lat;'} = 1; 
print_r($obj); 

Olacak çıktı

stdClass Object 
(
    [result[0]->geometry->location->lat;] => 1 
) 

Gördüğünüz gibi, bu tek mülkü, bir iç içe nesne grafiktir. Eğer XPath kullanmak ve dinamik bir nesneye erişmek için gereksinim mümkün değilseniz

$xml->result[0]->geometry->location->lat; 
1

kullanabileceğiniz şunlardır:

gibi XPath kullanabilir veya doğrudan istenilen değere gitmek ya Yorum önerilen yaklaşım: Kullanım [SimpleXMLElement :: xpath] (http://www.php.net/manual/simplexmlelement.xpath.php) yerine php itiraz notasyon

$oObj = new StdClass; 
$oObj->Root->Parent->ID = 1; 
$oObj->Root->Parent->Child->ID = 2; 

$sSeachInTree = 'Root\\Parent\\Child\\ID'; 
$aElements = explode("\\",$sSeachInTree); 

foreach($aElements as $sElement) 
{ 
    if (isset($oObj->{$sElement})) 
    { 
     if (end($aElements) == $sElement)  
     { 
      echo "Found: " . $sElement . " = " . $oObj->{$sElement}; 
     } 
     $oObj = $oObj->{$sElement}; 
    } 
}