Facebook Graph API - how to get user country?

后端 未结 10 1602
心在旅途
心在旅途 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:08

    Use the php sdk:

    require 'facebook.php';

    $facebook = new Facebook(array( 'appId' => 'APPID', 'secret' => 'APPSECRET',
    'cookie' => true, ));

    Then ask for a signed request:

      $signed_request = $facebook->getSignedRequest();
      $country = $signed_request["user"]["country"];
    

    Now $country has a string such as "us" for United States and "uk" for United Kingdom etc.

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

    use the official Facebook Graph API /me?fields=hometown it will return you e.g. "Prague, Czech Republic"

    I believe you can explode the string by needle ',' and the last element should be the country

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

    According to Facebook's documentation, you can get the 2-letter ISO 3166-1 country code from the user's locale.

    Facebook locales follow ISO language and country codes respectively, concatenated by an underscore. The basic format is ''ll_CC'', where ''ll'' is a two-letter language code, and ''CC'' is a two-letter country code. For instance, 'en_US' represents US English.

    Example FQL:

    SELECT locale FROM user WHERE uid = me()
    

    Response:

    {
      "data": [
        {
          "locale": "en_US"
        }
      ]
    }
    
    0 讨论(0)
  • 2020-11-30 03:18

    One solution I have found, which is merely a workaround, is to use the GeoNames API - http://www.geonames.org/export/web-services.html

    Using something like:

    http://api.geonames.org/search?q=houston%2C%20texas&featureClass=P&username=myusername
    

    Returns results in an xml object, with the country as part of the data.

    I'm not particularly happy with this as a solution however, as it adds an unnecessary layer of complications on to my application - plus requests to this free services are limited to 2000 per hour and 30,000 per day.

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

    for iframe apps, then ip is not hidden by FB servers, meaning you can do ip address lookup

    http://phpweby.com/software/ip2country

    the script was a bit buggy for me but did the job.

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

    https://api.facebook.com/method/fql.query?format=json&query=SELECT%20current_location%20FROM%20user%20WHERE%20uid=4&access_token=<ACCESS_TOKEN>

    [
      {
        "current_location": {
          "city": "Palo Alto",
          "state": "California",
          "country": "United States",
          "zip": "",
          "latitude": 37.4441,
          "longitude": -122.163,
          "id": 104022926303756,
          "name": "Palo Alto, California"
        }
      }
    ]
    

    FQL Documentation

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