Facebook Graph API - how to get user country?

后端 未结 10 1603
心在旅途
心在旅途 2020-11-30 02:56

I\'m developing a Facebook application using their Graph API. I have previously developed an app using the deprecated REST API.

I\'ve come across a problem in that

相关标签:
10条回答
  • 2020-11-30 03:22

    You should try like this:

    /* make the API call */
    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection,
                                                               NSDictionary<FBGraphUser> *user,
                                                               NSError *error)
        {
            if (!error)
            {
                self.countryName    = [[[user objectForKey:@"location"] valueForKey:@"name"] componentsSeparatedByString:@", "][1];
                self.countryCode    = [self getCountryCodeForCountry:STUser.countryName];
            }
    }];
    
    -(NSString *)getCountryCodeForCountry:(NSString*)country
    {
        NSMutableArray *countries = [self getCountries];
        NSArray *countryCodes = [NSLocale ISOCountryCodes];
    
        NSDictionary *codeForCountryDictionary = [[NSDictionary alloc] initWithObjects:countryCodes forKeys:countries];
    
        return [codeForCountryDictionary objectForKey:country];
    }
    
    +(NSMutableArray *)getCountries
    {
        NSMutableArray *countries = [NSMutableArray array];
    
        NSArray *countryCodes = [NSLocale ISOCountryCodes];
        countries = [NSMutableArray arrayWithCapacity:[countryCodes count]];
    
        for (NSString *countryCode in countryCodes)
        {
            NSString *identifier = [NSLocale localeIdentifierFromComponents:[NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
            NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
            [countries addObject: country];
        }
    
        return countries;
    }
    
    0 讨论(0)
  • 2020-11-30 03:23

    That is a good question.

    The problem is that any application run under facebook has to go through facebook servers - fb proxy and because of this the IP you get is not that of visitors but facebook's servers' instead which is of no use.

    I have not seen any solution for this yet except for getting user's country from his profile which again is only possible if user had made it public or authorized the permission for it.

    Here is useful link you might want to check out there too:

    • http://fbexchange.net/
    0 讨论(0)
  • 2020-11-30 03:23

    Answer posted by Valerchik is the right one indeed, but in case someone would look for PHP solution, then I ended up with something like this:

    $token = Yii::app()->facebook->getAccessToken();
    $fqlRequest = 'https://api.facebook.com/method/fql.query?query=' .
                  'SELECT%20' . $fqlAttributeName . '%20FROM%20user%20WHERE%20' .
                        'uid=' . $fbUserId . '&access_token=' . $token;
    $ret = file_get_contents($fqlRequest);
    
    if (!empty($ret)) {
        ...
    

    List of all user's attributes can be found here:
    https://developers.facebook.com/docs/reference/fql/user/

    I hope it will help someone :)

    0 讨论(0)
  • 2020-11-30 03:29

    You can see the lifetime number of followers (by likes to your FB page) by country using an FQL query. Set up a JSON query using the tokenized session value, and send an FQL query through via PHP with the object_ID of the page you are targeting and period of lifetime. select metric, and set the metric to page_fans_country - and dump the data into a CSV file. You should get back a list of total lifetime followers (by likes) by country code (multiple rows). Setup a pivot table to get an aggregated index across multiple pages of data, and rank by country code. Here's example of the FQL query:

    SELECT metric, value FROM insights WHERE object_id=[your page ID goes here] AND metric='page_fans_country' AND end_time=end_time_date('2013-04-28') AND period=period('lifetime')

    0 讨论(0)
提交回复
热议问题