Topic-icon PHP to Get Users First & Last Name?

Active Subscriptions:

None
Is it possible to get the first and last name of someone who is logged into Joomla using Facebook? I'm currently using this to get their email:
{$my->name}

Which as I understand, is legacy support for the old Joomla way of getting user attributes. This method also works:
$user = JFactory::getUser();
return $user->get('name');

Is there something similar that I can use for the users first and last name?
The topic has been locked.
Support Specialist
14 years 4 weeks ago #22440 by alzander
Ryan.. need more info. The Joomla 'name' should be the users name from Facebook, since we import that automatically. However, if you think they are different or for some reason just want the first name and it 'needs' to be from Facebook, it's pretty easy to get. Let me know what should happen if the user isn't logged in using Facebook as well though, so we get the right code.

Thanks,
Alex
The topic has been locked.
Active Subscriptions:

None
14 years 4 weeks ago #22441 by fb_1565926376
Oops, sorry about that, looks like I copied the wrong bit of code.... in my first post, "name" was supposed to be "email", which is how that code is returning an email address for me. :)

Essentially, I have a form with fields for First Name, Last Name and Email Address. Users who find this form will be logged in using facebook credentials, so it'd be nice to pre-populate the fields with their info. So I need to break out Joomla's "name" field into "first name" and "last name", but if there's a way to pull it from facebook using JFBC, that works too.
The topic has been locked.
Support Specialist
14 years 4 weeks ago #22447 by alzander
Here ya go:
$jfbcLibrary = JFBConnectFacebookLibrary::getInstance();
$fbUserId = $this->getFbUserId();
$fbUser = $this->_getUserName($fbUserId);
$first = $fbUser['first_name'];
$last = $fbUser['last_name'];
That will get the info from Facebook. I'd probably recommend that, if you can guarantee they're logged in. If you use the Joomla name, if the user filled the name field out with just "Joe" and no last name, you're out of luck. If you want though, it's really easy to get the Joomla name and break it into 2 parts using the space as a separator. Just let me know.

Hope that helps,
Alex
The topic has been locked.
Active Subscriptions:

None
14 years 4 weeks ago #22481 by fb_1565926376
Thanks Alex! I tried the following, but it threw some sort of PHP error, the entire page went white:
$jfbcLibrary = JFBConnectFacebookLibrary::getInstance();
$fbUserId = $this->getFbUserId();
$fbUser = $this->_getUserName($fbUserId);
$first = $fbUser['first_name'];
return $first;

Not sure what went wrong, but this code is run through the PHP eval function, so maybe it wasn't returning a string.... ? Either way, I tried your suggestion of breaking up the user's Name using explode and that worked great! Here's the code if anyone else needs it:
$user =& JFactory::getUser();
$name = $user->get('name');
$pieces = explode(" ", $name);
return $pieces[0];
The topic has been locked.
Support Specialist
14 years 4 weeks ago #22509 by alzander
Ryan,
Not sure what was wrong either. Glad you got your method going though. Should you need help with our recommendation though, just let me know!

Thanks,
Alex
The topic has been locked.