0

I want to fetch user's country by user's hometown.

I am using the latest Graph API v2.3 and I know FQL is deprecated in this version, so I can no longer use Facebook Graph API - how to get user country?

The only way is to do it by Graph API, but the tricky thing is

$fb_user = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());

var_dump($fb_user->getProperty('hometown'));

only returns the location Page id and name, then I do

$graph_obj_hometown = (new FacebookRequest($session, 'GET', '/' . $hometown_id, array('locale' => 'en_US')))->execute()->getGraphObject();

var_dump($graph_obj_hometown->asArray());

there is no country information in it, anyone knows how to get user's country?

Also, I have tried to get user hometown as a GraphLocation object then maybe I can use getCountry() method but it is still not working

$fb_user->getProperty('hometown', 'Facebook\GraphLocation');

the object only stored location name and location page id in the object.

Can anyone please help me?

Community
  • 1
  • 1
Jerr Wu
  • 97
  • 1
  • 1
  • 8
  • You have to ask for the field first – WizKid Apr 07 '15 at 05:08
  • @WizKid is right - try me/fields=hometown and make sure your app granted the permission user_hometown? If so you should get the pageId of the hometown in the following format: {"hometown": {"id": "110769888951990","name": "Dublin, Ireland"},"id": "772082842826394"} – Neil Apr 07 '15 at 05:11
  • @Neil yes I already did, so I said "returns the location Page id and name" – Jerr Wu Apr 07 '15 at 05:46
  • @WizKid I already did – Jerr Wu Apr 07 '15 at 05:48
  • The `country` field is not mandatory. If you're lucky, it's there, if not, it's not. – Tobi Apr 07 '15 at 10:10

1 Answers1

0

After getting the hometown page id, query: https://graph.facebook.com/v2.5/HOMETOWN_ID?fields=location The resulting object will contain the country.

Jack
  • 269
  • 2
  • 6