Daniel Benzie
Daniel Benzie

Reputation: 479

Getting user country Facebook API PHP SDK

Currently having a few issues accessing the country from a given user on facebook. I have requested the user_location permission and my graph API call also requests location however I am only ever returned the city and an ID for the location - never an actual country.

My requests etc are below. I am using the standard PHP SDK docs

// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me?fields=birthday,name,statuses,photos,location' );
$response = $request->execute();

// get response
$response = $response->getGraphObject();



 $data_we_need = array();

  $data_we_need['name'] = $response->getProperty('name');
  $data_we_need['birthday'] = $response->getProperty('birthday');
  $data_we_need['location'] = $response->getProperty('location');


  $statuses = $response->getProperty('statuses');
  $data_we_need['statuses'] = $statuses->asArray();

  $photos = $response->getProperty('photos');
  $data_we_need['photos'] = $photos->asArray();

I am returned an results like:

  [name] => xxxxxx
    [birthday] => 05/14/1990
    [location] => __PHP_Incomplete_Class Object
        (
            [__PHP_Incomplete_Class_Name] => Facebook\GraphObject
            [backingData:protected] => Array
                (
                    [id] => 112087812151796
                    [name] => Gloucester, Gloucestershire
                )

        )

I need to be able to get country from the location data provided.

Any help would be massively appreciated.

Upvotes: 2

Views: 1005

Answers (1)

Tom Tom
Tom Tom

Reputation: 3698

As far as I know the location & hometown fields are user inputs (community pages), hence you won't get stable results using the facebook API. You might rather want to try detecting the country yourself with the IP.

Upvotes: 2

Related Questions