Facebook API, retrieve first name

1

I am using version 2.9 of Api Facebook in PHP, my goal is to recover the most basic information.

From what I read in the documentation, the only authorization I would need is the email, which I already have. First Name and Last Name, would not require additional authorization

But when you run

$loader = require __DIR__ . '\..\vendor\autoload.php';
    $token = $_POST['token'];
    include './Facebook/Facebook.php';
    $fb = new \Facebook\Facebook([
        'app_id' => 'meu id aqui',
        'app_secret' => 'meu segredo aqui',
        'default_graph_version' => 'v2.9',
        'default_access_token' => $token
    ]);

    $helper = $fb->getRedirectLoginHelper();

    $response = $fb->get('/me',$token);
    $me = $response->getGraphUser();
    echo json_encode(array(
        "resultado" => true, 
        "id" => $me->getId(),
        "name" => $me->getName(), 
        "fields" => $me->getFieldNames(),
        "email" => $me->getEmail(),
        "cover" => $me->getField("cover"),
        "firstname" => $me->getFirstName(),
        "lastname"=>$me->getLastName(),
        "picture" => $me->getPicture()

));

I only receive

  • id
  • name
  • email
  • All other fields are null

    When passing by parameter in get('/me?fields=email,name,id,lastname,firstname') , PHP issues the error

      Graph returned an error: (# 100) Tried accessing nonexisting field (lastname) on node type (User) *

    How to retrieve this information with the API?

        
    asked by anonymous 17.05.2017 / 06:06

    1 answer

    2

    Use the Graph API Explorer to test your queries before programming:

    >

    When you add more fields you can check the correct values, you are using lastname but the right is last_name :

        
    17.05.2017 / 09:09