smith.james0 Posted May 21, 2013 Share Posted May 21, 2013 I am trying to get the longitude and latitude from yahooapis using http://query.yahooapis.com/v1/public/yql?q=select%20centroid%20from%20geo.places%20where%20text%3D%22$postcode%2Cuk%22&diagnostics=true where $postcode is a uk postcode, but I get nothing. I know it's something simple but I can't get it to work $doc = new DOMDocument(); $doc->load("http://query.yahooapis.com/v1/public/yql?q=select%20centroid%20from%20geo.places%20where%20text%3D%22$postcode%2Cuk%22&diagnostics=true"); $channel = $doc->getElementsByTagName("*"); $info = array(); foreach($channel as $element) { // Condition if($element->localName == "centroid") { $latlongpostcode['latitude'] = $element->getElementsByTagName("latitude"); $latlongpostcode['longitude'] = $element->getElementsByTagName("longitude"); } } } print_r($latlongpostcode); <?xml version="1.0" encoding="UTF-8"?> <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2013-05-21T16:19:29Z" yahoo:lang="en-US"> <diagnostics> <publiclyCallable>true</publiclyCallable> <url execution-start-time="1" execution-stop-time="135" execution-time="134"><![CDATA[http://where.yahooapis.com/v1/places.q(wa37pq%2C%20uk);start=0;count=10]]></url> <user-time>136</user-time> <service-time>134</service-time> <build-version>36794</build-version> </diagnostics> <results> <place xmlns="http://where.yahooapis.com/v1/schema.rng"> <centroid> <latitude>53.414478</latitude> <longitude>-2.525600</longitude> </centroid> </place> </results> </query> Any help would be great full James Quote Link to comment Share on other sites More sharing options...
smith.james0 Posted May 21, 2013 Author Share Posted May 21, 2013 Not sure if it's the best way but I have got it to work $postcode = str_replace(' ','',$postcode); $doc = new DOMDocument(); $doc->load("http://query.yahooapis.com/v1/public/yql?q=select%20centroid%20from%20geo.places%20where%20text%3D%22$postcode%2Cuk%22&diagnostics=true"); $channel = $doc->getElementsByTagName("*"); $info = array(); foreach($channel as $element) { // Condition if($element->localName == "centroid") { $latlongpostcode['latitude'] = $element->getElementsByTagName('latitude')->item(0)->nodeValue; $latlongpostcode['longitude'] = $element->getElementsByTagName('longitude')->item(0)->nodeValue; } } } Quote Link to comment Share on other sites More sharing options...
Barand Posted May 21, 2013 Share Posted May 21, 2013 (edited) or $xml = simplexml_load_file("http://query.yahooapis.com/v1/public/yql?q=select%20centroid%20from%20geo.places%20where%20text%3D%22$postcode%2Cuk%22&diagnostics=true"); $location = (array)$xml->results->place->centroid; /**** RESULT *************** $location = Array ( [latitude] => 54.375240 [longitude] => -2.594900 ) ****************************/ Edited May 21, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
smith.james0 Posted May 22, 2013 Author Share Posted May 22, 2013 Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.