views:

191

answers:

2

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:

+1  A: 

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

Jamal Fanaian
Thanks... but do you have any experience pulling email addresses using Zend? How well does it work with the different providers?
philfreo
+1  A: 

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.

Tim Lytle
Thanks... but do you have any experience pulling email addresses using Zend? How does it work with the different providers?
philfreo
I've added example code from the Zend Documentation.
Tim Lytle
So basically you still have to try to get the data with SR, and then attempt AX if that fails?
philfreo
I think you may be able to request both (initially) with the JanRain library.
Tim Lytle