pimax / fb-messenger-php

Facebook Messenger Bot PHP API
GNU General Public License v2.0
314 stars 147 forks source link

Undefined index: first_name #54

Closed SoulArcangel77 closed 7 years ago

SoulArcangel77 commented 7 years ago

Hi, when i use the function FbBotApp::userProfile($messenger_id) my script stops with the error:

[ErrorException]
Undefined index: first_name

How do i use FbBotApp::userProfile witouth stop my script?

SoulArcangel77 commented 7 years ago

probable solution in UserProfile.php

namespace pimax;

class UserProfile
{
    protected $data = [];

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function getFirstName()
    {
        return isset($this->data['first_name']) ? $this->data['first_name'] : null;
    }

    public function getLastName()
    {
        return isset($this->data['last_name']) ? $this->data['last_name'] : null;
    }

    public function getPicture()
    {
        return isset($this->data['profile_pic']) ? $this->data['profile_pic'] : null;
    }

    public function getLocale()
    {
        return isset($this->data['locale']) ? $this->data['locale'] : null;
    }

    public function getTimezone()
    {
        return isset($this->data['timezone']) ? $this->data['timezone'] : null;
    }

    public function getGender()
    {
        return isset($this->data['gender']) ? $this->data['gender'] : null;
    }
}
wittfabian commented 7 years ago

Can you give an example?

For me, the following code works

$user = $bot->userProfile('FACEBOOK_USER_ID');
echo $user->getFirstName() ." ". $user->getLastName();

The following function call is not possible: FbBotApp::userProfile($messenger_id)

SoulArcangel77 commented 7 years ago

I have created a script that send a newsletter to all my contac who use messenger, so now i have 800 messenger_id of which approximately 100 users not have setted the first_name. When userProfile not find the first_name, the script stops with error!

I have solved using isset in UserProfile.php

wittfabian commented 7 years ago

Ok, thanks. I will fix it. Maybe an empty string is better than null

SoulArcangel77 commented 7 years ago

For those who use the function is_null(), is better null, for those who use the function empty() is indifferent :)

wittfabian commented 7 years ago

So, we use null