Php-graph-sdk: Undefined index 'email' in \Facebook\GraphNodes\Collection when e-mail requested, but not provided

Created on 8 Nov 2017  路  11Comments  路  Source: facebookarchive/php-graph-sdk

I'm trying to fetch user data from Facebook. Users log in with Facebook Login using JS SDK (scope: "public_profile,email").
After sucessful login, there is the code:

    $obj_fb = new \Facebook\Facebook([
        'app_id' => '11.....111',
        'app_secret' => 'xxxx',
        'default_graph_version' => 'v2.10'
    ]);

    $helper = $obj_fb->getJavaScriptHelper();

    try {
        $accessToken = $helper->getAccessToken();
        $obj_fb->setDefaultAccessToken($accessToken);
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }

    if (!isset($accessToken)) {
        echo 'No cookie set or no OAuth data could be obtained from cookie.';
        exit;
    }

    $response = $obj_fb->get('/me?fields=id,name,email,first_name,last_name');
    $user_data = $response->getGraphUser();

    echo 'User e-mail: ' . $user_data['email'];

If user doesn't provide an e-mail (see: https://developers.facebook.com/bugs/298946933534016/), FB PHP SDK prints a notice:

PHP Notice:  Undefined index: email in /home/virtual/foobar.com/www/vendor/facebook/graph-sdk/src/Facebook/GraphNodes/Collection.php on line 201

Expected: return empty string or an exception.

Most helpful comment

I have the same issue. This is the only notice in my project since everything else i have cleaned up.

All 11 comments

@czachor thanks for the report!
I think you should either wrap your call with if (isset($user_data['email'])) (which is common with arrays) or use $user_data->getField('email')

@yguedidi Tested, $user_data->getField('email') produces the same error message :(

I have the same issue. This is the only notice in my project since everything else i have cleaned up.

@czachor weird... look at the getField() body, it should return null if the key doesn't exist

Hi @yguedidi
I think the problem is not the index or the methods to prevent the error.
I my case (maybe could be @czachor, @webmasterlv also) the user granted the permission, but the email does not been returned.
I've check all declined permissions then I call $helper->getReRequestUrl.
So, all permissions I asked are granted or the user cannot login.

image

I've noted for some those users these permissions are not returned, only firends can have information about the user, and the profile on facebook is displayed like that (Look the "_To see what he shares with friends, send him a friend request._"):
image
image

It was plausible the profile omited for non-friends could not return some info.
But, I had two users which the facebook profile is 'wide opened' (see next picture).
For these users, the permission 'friends' returned info.
But email was not returned.
It happens with 4 from 30 users in one day.
image
image

Thanks all for all thoses details!

Here what the doc says:
image

So I'm closing this as it looks like not and SDK issue, but a user who changes his/her email adress to an invalid one.

I'm sorry, but as i understand (please correct me if I'm wrong), it is the SDK issue in a way that it produces a PHP notice. In my project, log file is constantly filling up with these notices. Sure, application itself must handle cases when email is not present/invalid etc., but SDK shouldn't throw a simple notice about trying to access non-existent variable. This is a simple matter of checking with isset() or empty().

@webmasterlv the SDK already use isset() (see there).
The fact that you get a notice it's because you rely on the array access notation ($user_data['email']) without checking if the key exists.

A solution to your issue in te SDK would be to make offsetExists() (there) always return true, so you never get a notice, but we won't.
You should always check for the existance of an array key in your own code, and we will keep the SDK enforcing this best practice.

If you don't want notices anymore: https://github.com/facebook/php-graph-sdk/issues/882#issuecomment-351553723 :)

@yguedidi

The fact that you get a notice it's because you rely on the array access notation ($user_data['email']) without checking if the key exists.

Oh, that was the problem, i added isset() in my code, that fixed it. Thank you for support :)

@webmasterlv you're welcome

Still happened in 2011.

Collection.php on line 201

Log:

2021/01/19 17:26:49 [error] 31862#31862: *36698 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined index: email in /home/example.site/web/vendor/facebook/graph-sdk/src/Facebook/GraphNodes/Collection.php on line 201" while reading response header from upstream, client: 118.200.1.11, server: example.site, request: "GET /activity/example/oauth/callback/facebook?code=AQCFeNXCdOmmhg4l8Hz3i1WlB47hkHXvAkF4YQgTdfeQEjrlB1BUXCgonvrxSqp8bfk7k1gdSkDPkKmmPnhnU88bjT5R7bJCcxQ7fF2yVJKKcG73t5XMRulXLgppbUB2OpCBcKgFmh3CBKdRYjwsOeGBHyPU9fClJ-xFLApJTdajEuP_5LM53tMWfiYMKSSNCiRqPALjEpP-GwcDEnoEW_RGRodB3GKvcf8nreOx2dclB4xQhASyA0Liux3Z-vlajKyL5b5Uukazdz2zgOjrabn2Odq6vQyeoRtsO3qck4ncpjXccC-Fhd542m300eXkxolH6vxvy0ExjKAcnEQgCd4g&state=d1bfb348e6e4e46138a298314f5cbfba HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "example.site", referrer: "https://m.facebook.com/"

Problem line:

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corvus87 picture corvus87  路  7Comments

glensc picture glensc  路  8Comments

JenyaInfiapps picture JenyaInfiapps  路  6Comments

shengfa picture shengfa  路  7Comments

mohsenshakibafar picture mohsenshakibafar  路  4Comments