Requesting email address from OpenID provider in PHP

744 views Asked by At

What's the simplest way to request an email address from an OpenID provider?

Is there a good PHP library that simplifies this problem?

I know that providers implement things differently. I've heard you need to do both Simple Registration and Attribute Exchange. I'd particularly want to make sure that it worked with the biggest providers, such as MyOpenID and Google.

Related, but inadequate, questions:

2

There are 2 answers

3
Tim Lytle On BEST ANSWER

JanRain has a Open ID PHP Library, one of the first libraries (I believe) from a company centric to OpenID. You mentioned working with MyOpenID, which JanRain's site.

That said, personally I use the Zend Library now (I played with the JanRain library years ago), just because I generally develop using the Zend Framework.

Here are some code examples if the provider uses the OpenID Simple Registration Extension.

This section of the Zend Reference guide shows how to request user information with the authentication request. Here's their example code edited for your needs:

//require e-mail, get nickname and fullname if available
$sreg = new Zend_OpenId_Extension_Sreg(array(
    'nickname'=>false,
    'email'=>true,
    'fullname'=>false), null, 1.1);
$consumer = new Zend_OpenId_Consumer();
if (!$consumer->login($openid, $returnUrl, null, $sreg)) {
    die("OpenID login failed.");
}

Accomplishing the same with the JanRain library is similar, here's some code taken from from the try_auth.php file of the library package (I edited it to show the basic functions you're looking for):

$auth_request = $consumer->begin($openid);
$sreg_request = Auth_OpenID_SRegRequest::build(
                                 // Required
                                 array('email'),
                                 // Optional
                                 array('fullname', 'nickname'));
$auth_request->addExtension($sreg_request);

For Attribute Exchange check out the JanRain Auth_OpenID_AX Classes, in the Zend Library there's a feature request for AX support.

1
Jamal Fanaian On

I would look at Zend Framework's OpenID Library, Zend_OpenID. You should be able to use it without the entire framework.