PHP:LightOpenID,如何从提供商处获取帐户数据?

时间:2010-07-05 11:44:52

标签: php openid provider fetch lightopenid

我遇到一个名为LightOpenID的小型OpenID库出现问题 。 我可以对几乎所有提供商进行身份验证,但我不知道如何从提供程序获取数据。我只使用print_r()得到了Array(),eaven。

2 个答案:

答案 0 :(得分:8)

您需要在之前 getAttributes()之后致电$openid->validate()

记住:

  

请注意,它不保证会出现任何必需/可选参数

答案 1 :(得分:2)

这就是我使用它的方式。这是lightopenid文件夹中的文件openid.php。在课堂上进行以下附加功能 -

class LightOpenID
{
    public $returnUrl
         , $required = array()
         , $optional = array()
         , $verify_peer = null
         , $capath = null
         , $cainfo = null;

    // these are the variables which store the data about the user...
    public function ret_fname() { return $this->data['openid_ext1_value_namePerson_first']; }
    public function ret_lname() { return $this->data['openid_ext1_value_namePerson_last']; }
    public function ret_email() { return $this->data['openid_ext1_value_contact_email']; }
    public function ret_lang() { return $this->data['openid_ext1_value_pref_language']; }
}



现在将您的文件示例设为login.php,在您要进行身份验证时调用该文件。对于不同的身份验证域,可能会有多个此文件的副本。

<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
session_start();
require 'lightopenid/openid.php';
include_once('config.php');                // initial setting file

try {

    $openid = new LightOpenID;                            // akshat - declared an object of class lightopenid.. this is listed in openid.php
    if(!$openid->mode) {

        if(isset($_GET['login'])) {

            $openid->identity = 'https://www.google.com/accounts/o8/site-xrds?hd=YourDomain.in'; //this can be changed as you know...       

            $openid->required = array('namePerson/friendly', 'contact/email' , 'contact/country/home', 'namePerson/first', 'pref/language', 'namePerson/last'); // akshat - line added by me from after reading from the net....

            header('Location: ' . $openid->authUrl());    
        }
?>
<script type="text/javascript" src="js/jquery-1.4.2.min.js" language="javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
            document.form.submit();
    });

</script>
<form name="form" action="?login" method="post"> </form>
<?php
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication for Your Domain !';
    } else {                                                // FETCH USER INFO HERE
                $fname = $openid->ret_fname();              // fetching user first name...
        $lname = $openid->ret_lname();                  // fetching user last name...
        $email = $openid->ret_email();                  // fetching user email...
        $lang = $openid->ret_lang();                    // fetching user language...
                session_start();

                // use it as required. I set them in session !
                $_SESSION['admin']['emailID'] = $email;          //put email id in session.
                $_SESSION['admin']['fname'] = $fname;            //put first name also in session.
                $_SESSION['admin']['lname'] = $lname;            //put last name also in session.
                $rurl = $_SESSION['redirect'];                   // you can ignore this. Go to your own page now...
                header("Location:$rurl");               // Go back to the calling application !

    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>