Jump to:

23358 Posts in 17874 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » restful service getValues -- I cant get any values

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1029 Views
  • theoldlr
    Avatar
    Community Member
    103 Posts

    restful service getValues -- I cant get any values Link to this post

    I'm trying to use restful service with google maps to get the latitude and longitude of a given address.

    function onBeforeWrite(){

    $map = new RestfulService("http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false", 0);

    $xmlResponse=$map->request()->getBody();

    //$coordinates = $map->getValues($xmlResponse,"location");
    //$lat = $map->getValues($xmlResponse,"GeocodeResponse_result_geometry_location","lat");
    //$long = $map->getValues($xmlResponse,"result_geometry_location","lng");

    parent::onBeforeWrite();

    }

    None of the commented getValues() above returns anything but null--obviously I've tried several different ways. The xml response is along the lines of (full response here):

    <GeocodeResponse>
    <status>OK</status>
    <result>
    <a lot of address components the same level as geometry></a lot of address components the same level as geometry>
    <a lot of address components the same level as geometry></a lot of address components the same level as geometry>
    <geometry>
    <location>
    <lat>37.4217550</lat>
    <lng>-122.0846330</lng>
    </location>
    <location_type>ROOFTOP</location_type>
    <viewport>
    <southwest>
    <lat>37.4188514</lat>
    <lng>-122.0874526</lng>
    </southwest>
    <northeast>
    <lat>37.4251466</lat>
    <lng>-122.0811574</lng>
    </northeast>
    </viewport>
    </geometry>
    </result>
    </GeocodeResponse>

    What am i doing wrong?
    Thank you!

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: restful service getValues -- I cant get any values Link to this post

    I' got this to work using the RestfulService::searchValue() method instead of getValues(). This is a little testversion I did in my Page_Controler (using your location ):

    function RestfulServiceTest() {
          $map = new RestfulService("http://maps.googleapis.com/maps/api/geocode/xml");
       
          $params = array(
             'address' => '1600 Amphitheatre Parkway, Mountain View, CA',
             'sensor' => 'false'
          );
          $map->setQueryString($params);
       
          $xmlResponse = $map->request()->getBody();
       
          $node = '/GeocodeResponse/result/geometry/location/';
       
          $lattitude = $map->searchValue($xmlResponse, $node . 'lat');
          $longitude = $map->searchValue($xmlResponse, $node . 'lng');

    return "Lattitude: {$lattitude }, longitude: {$longitude}<br />";
    }

    Hope this helps,
    Martine

  • theoldlr
    Avatar
    Community Member
    103 Posts

    Re: restful service getValues -- I cant get any values Link to this post

    Thanks, martimiz!

    That worked perfectly. For the sake of understanding, anyone have an idea why the getValues() wasn't working besides some of the wrong syntax i was using? (still doesnt work with correct syntax) I saw working examples on the forum (though they were not looking for values buried so deep in the xml) that used getValues()--Something about the way google formats the response maybe?

    1029 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.