How to retrieve location specific fields from Facebook Graph API
Recently I stumbled upon the small issue regarding the object returned by Facebook Graph API. Our app was relatively simple allowing us to get user age and location in order to determine whether user was allowed to see the site content or not.
We used PHP SDK for that purpose and fetched the data using get method. We wanted to get the information in one query for obvious performance reasons.
Our get URL looked like
/me?fields=age_range,location{location}
And response provided was something like (example simplified):
"age_range": {
"min": "21"
},
"location": {
"location": {
"city": "some city",
"country": "some location",
"latitude": 35.000,
"longitude": 15.123
}
}
So everything nice and perfect? Not really.
Unfortunately we could not match country names with our internal system and location object from Facebook was not containing country ISO code. We were unable to modify our internal system records as it was used across many other systems.
Official Graph API documentation was not providing any examples regarding how to fetch more information. Of course there was a way to make == 2 API calls == and then merge the information together but this wasn't something we were seeking for (performance, stupid).
As always in those kind of situations I have tried looking on Stack Overflow in case someone already had this issue before. Unfortunately answers were not helpful (this, that or that one) and still there was no solution to our issue.
With no more options on hand I started fooling around with the URL parameters. After short trial and error it worked! (you love that moment too, don't you? :) )
As it looks like the answer is pretty straightforward -> You can nest your get parameters in order to fetch more properties of the objects.
By modifying URL to look like
/me?fields=age_range,location{location{country_code}}
you can get the response which contains all needed information.
I hope that this helps someone!