[英]Yahoo open id login api
我是新的開放ID,但我仍然試圖在我的網站中集成開放的ID。 我正在使用雅虎提供商登錄我的網站。 可以幫助我獲取用戶信息,如雅虎郵件ID,名字,姓氏。
我從谷歌提供的網站上獲取以下代碼,我已將提供商更改為雅虎
我試過的代碼:
<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'openid.php';
try {
# Change 'localhost' to your domain name.
$openid = new LightOpenID('localhost');
if(!$openid->mode) {
if(isset($_GET['login'])) {
$openid->identity = 'https://me.yahoo.com';
header('Location: ' . $openid->authUrl());
}
?>
<form action="?login" method="post">
<button>Login with Yahoo</button>
</form>
<?php
} elseif($openid->mode == 'cancel') {
echo 'User has canceled authentication!';
} else {
echo 'User ' . ($openid->validate() ? $openid->identity . ' has ' : 'has not ') . 'logged in.';
print_r($openid->ax_to_sreg);
}
} catch(ErrorException $e) {
echo $e->getMessage();
}
loggin后出來就像這樣:
User https://open.login.yahooapis.com/openidx20/user_profile/XXXX has logged in.
上面的url有什么用? 我如何獲取用戶信息?
經過谷歌搜索后找到我的解決方案。 現在我能夠獲取登錄用戶的電子郵件
這是修改后的代碼:
<?php
require 'openid.php';
try
{
# Change 'localhost' to your domain name.
$openid = new LightOpenID($_SERVER['HTTP_HOST']);
//Not already logged in
if(!$openid->mode)
{
//do the login
if(isset($_GET['login']))
{
//The google openid url
$openid->identity = 'https://me.yahoo.com';
//Get additional google account information about the user , name , email , country
$openid->required = array('contact/email' , 'namePerson/first' , 'namePerson/last' , 'pref/language' , 'contact/country/home');
//start discovery
header('Location: ' . $openid->authUrl());
}
else
{
//print the login form
login_form();
}
}
else if($openid->mode == 'cancel')
{
echo 'User has canceled authentication!';
//redirect back to login page ??
}
//Echo login information by default
else
{
if($openid->validate())
{
//User logged in
$d = $openid->getAttributes();
$first_name = $d['namePerson/first'];
$last_name = $d['namePerson/last'];
$email = $d['contact/email'];
$language_code = $d['pref/language'];
$country_code = $d['contact/country/home'];
$data = array(
'first_name' => $first_name ,
'last_name' => $last_name ,
'email' => $email ,
);
//now signup/login the user.
print_r($data);
}
else
{
//user is not logged in
}
}
}
catch(ErrorException $e)
{
echo $e->getMessage();
}
/*
This function will print the login form with the button
*/
function login_form()
{
?>
<a href="?login">Login with Yahoo</a>
<?php
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.