Ad

PHP Facebook GET Request - Gender Missing

I am working on a project that requires grabbing a Facebook users gender, provided by the 'user_gender' scope. At the moment whilst still in production I am making use of Test Users and have been having issues with getting the gender.

The test user's I have created 100% have the 'user_gender' scope which is reflected in Facebook's own GraphQL Explorer showing me the data I require, including the gender. A dump of the returned JSON is below.

Request: /me?fields=first_name,last_name,email,gender
{
  "first_name": "Ullrich",
  "last_name": "Brownman",
  "email": "[email protected]",
  "gender": "male",
  "id": "109073989989984"
}

So far, so good. My problems arise when I am trying to grab the gender from the PHP SDK, more specifically the LaravelFacebookSdk (https://github.com/SammyK/LaravelFacebookSdk).

Below is some code extracts.

$response = $fb->get('/me?fields=first_name,last_name,email,gender', $userCredentials['access_token']);

return response()->json([
      'first_name' => $response->getGraphUser()->getFirstName(),
      'last_name' => $response->getGraphUser()->getLastName(),
      'email' => $response->getGraphUser()->getEmail(),
      'fb_id' => $response->getGraphUser()->getId(),
      'gender' => $response->getGraphUser()->getGender()
    ], StatusCode::OK);

This is what the returned JSON looks like:

{
    "first_name": "Ullrich",
    "last_name": "Brownman",
    "email": "[email protected]",
    "fb_id": "109073989989984",
    "gender": null
}

Just to confirm, the access token I am using in the Explorer and in the PHP code is exactly the same. Same permissions, same user. As you can see, all but the gender is displayed and correct.

I have tried to dump the 'gender' field and nothing is displayed. Doing this $response->getGraphUser()->all() shows that nothing is being pulled for gender which makes me think this isn't a problem with my code and more likely a configuration problem.

Im not sure exactly where in the process this is going wrong but any pointers to debug would be greatly appreciated.

Ad

Answer

Answer was in the comments, posted for reference.

Which Graph API are you targeting with the GraphQL Explorer? The recent V3? Your package is targeting V2.10, which might cause some issues.

Also note that that package hasn't received updates for almost a year, so that can the culprit too.

Ad
source: stackoverflow.com
Ad