Using yahoo geolocation api to find woeid

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi All

I hope some one can help me.

I am using the yahoo geolocation api where I input the latitude and longitude of the city in question. With this I try to get the woeid of the city as now required by yahoo.

I get the contents and then print them to see where the woeid is located as follows:

Code: [ Select ]
$api_url = "http://where.yahooapis.com/geocode?location=".$cityLatitude.",".$cityLongitude."&flags=J&gflags=R&appid=glvQg--";

$response = file_get_contents($api_url);
print $response;
  1. $api_url = "http://where.yahooapis.com/geocode?location=".$cityLatitude.",".$cityLongitude."&flags=J&gflags=R&appid=glvQg--";
  2. $response = file_get_contents($api_url);
  3. print $response;


I get the following response which I think is xml output:

Code: [ Select ]
{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":99,"Found":1,"Results":[{"quality":99,"latitude":"43.866700","longitude":"-79.433300","offsetlat":"43.866700","offsetlon":"-79.433300","radius":500,"name":"43.8667,-79.4333","line1":"10 Canterbury Ct","line2":"Richmond Hill, ON L4C","line3":"","line4":"Canada","house":"10","street":"Canterbury Ct","xstreet":"","unittype":"","unit":"","postal":"L4C","neighborhood":"","city":"Richmond Hill","county":"York","state":"Ontario","country":"Canada","countrycode":"CA","statecode":"ON","countycode":"","hash":"","woeid":12697531,"woetype":11,"uzip":"L4C"}]}}


The woeid is located near the bottom so I need to parse this to get it.

Parsing is the problem. I can't seem to do it properly with the php functions.

I would appreciate any advice on how I can do this.

Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

The result you are getting back from Yahoo is actually in JSON format. I believe as long as you are using PHP greater than 5.2.0, the JSON functions in PHP will be available. See this:

http://www.php.net/manual/en/function.json-decode.php

So to parse those results, all you would need to do is:

PHP Code: [ Select ]
$my_array = json_decode($response, true);


I put the second argument as true so that it returns the result as an associative array.

If you need to see the structure of the array go ahead and do a print_r on it:

PHP Code: [ Select ]
print_r($my_array);


I went ahead and did a test for you and the array should look like this:

Code: [ Select ]
Array
(
  [ResultSet] => Array
    (
      [version] => 1.0
      [Error] => 0
      [ErrorMessage] => No error
      [Locale] => us_US
      [Quality] => 99
      [Found] => 1
      [Results] => Array
        (
          [0] => Array
            (
              [quality] => 99
              [latitude] => 43.866700
              [longitude] => -79.433300
              [offsetlat] => 43.866700
              [offsetlon] => -79.433300
              [radius] => 500
              [name] => 43.8667,-79.4333
              [line1] => 10 Canterbury Ct
              [line2] => Richmond Hill, ON L4C
              [line3] =>
              [line4] => Canada
              [house] => 10
              [street] => Canterbury Ct
              [xstreet] =>
              [unittype] =>
              [unit] =>
              [postal] => L4C
              [neighborhood] =>
              [city] => Richmond Hill
              [county] => York
              [state] => Ontario
              [country] => Canada
              [countrycode] => CA
              [statecode] => ON
              [countycode] =>
              [hash] =>
              [woeid] => 12697531
              [woetype] => 11
              [uzip] => L4C
            )

        )

    )

)
  1. Array
  2. (
  3.   [ResultSet] => Array
  4.     (
  5.       [version] => 1.0
  6.       [Error] => 0
  7.       [ErrorMessage] => No error
  8.       [Locale] => us_US
  9.       [Quality] => 99
  10.       [Found] => 1
  11.       [Results] => Array
  12.         (
  13.           [0] => Array
  14.             (
  15.               [quality] => 99
  16.               [latitude] => 43.866700
  17.               [longitude] => -79.433300
  18.               [offsetlat] => 43.866700
  19.               [offsetlon] => -79.433300
  20.               [radius] => 500
  21.               [name] => 43.8667,-79.4333
  22.               [line1] => 10 Canterbury Ct
  23.               [line2] => Richmond Hill, ON L4C
  24.               [line3] =>
  25.               [line4] => Canada
  26.               [house] => 10
  27.               [street] => Canterbury Ct
  28.               [xstreet] =>
  29.               [unittype] =>
  30.               [unit] =>
  31.               [postal] => L4C
  32.               [neighborhood] =>
  33.               [city] => Richmond Hill
  34.               [county] => York
  35.               [state] => Ontario
  36.               [country] => Canada
  37.               [countrycode] => CA
  38.               [statecode] => ON
  39.               [countycode] =>
  40.               [hash] =>
  41.               [woeid] => 12697531
  42.               [woetype] => 11
  43.               [uzip] => L4C
  44.             )
  45.         )
  46.     )
  47. )


So to access woeid you would do this:

PHP Code: [ Select ]
$woeid = $array['ResultSet']['Results'][0]['woeid'];  
echo $woeid;
  1. $woeid = $array['ResultSet']['Results'][0]['woeid'];  
  2. echo $woeid;


For me that outputs:

12697531

I hope that helps!
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Big

You are right, it was a json output

When I did as you said and decoded, I got the array.

I used [Result] not [Results]. No doubt a typo error.

When I looked further into the yahoo documetation, they mentioned a flag to set to get serialized php output.
I did this and then unserialize and got the same arry.

Now I have two ways to do it.

Thanks for all your help
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

No problem :)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 100 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.