Javascript 无法获取用户';使用PHPSDK的用户名
我能够获取所有用户的详细信息,除了我需要在我的站点中使用的用户用户名(在CI框架中编码)。我希望我的网站与facebook完全集成。 我用过PHPSDK代码。 下面我提供了完整的代码。请让我知道我是否可以使用用户的fb id和FQL获取用户的用户名Javascript 无法获取用户';使用PHPSDK的用户名,javascript,php,facebook-php-sdk,Javascript,Php,Facebook Php Sdk,我能够获取所有用户的详细信息,除了我需要在我的站点中使用的用户用户名(在CI框架中编码)。我希望我的网站与facebook完全集成。 我用过PHPSDK代码。 下面我提供了完整的代码。请让我知道我是否可以使用用户的fb id和FQL获取用户的用户名 <div id="fb-root"></div> <script type="text/javascript"> var button; var userInfo; window.fb
<div id="fb-root"></div>
<script type="text/javascript">
var button;
var userInfo;
window.fbAsyncInit = function() {
FB.init({ appId: 'your app id', //change the appId to your appId
status: true,
cookie: true,
xfbml: true,
oauth: true});
// showLoader(true);
function updateButton(response) {
button = document.getElementById('fb-auth');
userInfo = document.getElementById('user-info');
if (response.authResponse) {
//user is already logged in and connected
FB.api('/me', function(info) {
login(response, info);
//alert(info.email);
});
button.onclick = function() {
FB.logout(function(response) {
logout(response);
});
};
} else {
//user is not connected to your app or logged out
button.innerHTML = 'Login';
button.onclick = function() {
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(info) {
login(response, info);
});
}
else
{
//user cancelled login or did not grant authorization
showLoader(false);
}
}, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});
}
}
}
// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol
+ '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
function login(response, info){
if (response.authResponse) {
var accessToken = response.authResponse.accessToken;
userInfo.innerHTML = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name;
button.innerHTML = 'Logout';
showLoader(false);
document.getElementById('other').style.display = "block";
}
}
function logout(response){
userInfo.innerHTML = "";
document.getElementById('debug').innerHTML = "";
document.getElementById('other').style.display = "none";
showLoader(false);
}
//stream publish method
function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
showLoader(true);
FB.ui(
{
method: 'stream.publish',
message: '',
attachment: {
name: name,
caption: '',
description: (description),
href: hrefLink
},
action_links: [
{ text: hrefTitle, href: hrefLink }
],
user_prompt_message: userPrompt
},
function(response) {
showLoader(false);
});
}
function showStream(){
FB.api('/me', function(response) {
//console.log(response.id);
streamPublish(response.name, 'I like the articles of muffin.my', 'hrefTitle', 'http://muffin.my', "Share muffin.my");
});
}
function share(){
showLoader(true);
var share = {
method: 'stream.share',
u: 'http://muffin.my/'
};
FB.ui(share, function(response) {
showLoader(false);
console.log(response);
});
}
function graphStreamPublish(){
showLoader(true);
FB.api('/me/feed', 'post',
{
message : "I love muffin.my muffin muffin muffin",
link : 'http://muffin.my',
picture : 'http://muffin.my/iphone/lucky7_ios.jpg',
name : 'iOS Apps & Games',
description : 'muffin muffin muffin muffin muffin muffin muffin muffin muffin muffin muffin muffin muffin '
},
function(response) {
showLoader(false);
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
function fqlQuery(){
showLoader(true);
FB.api('/me', function(response) {
showLoader(false);
//http://developers.facebook.com/docs/reference/fql/user/
var query = FB.Data.query('select name, profile_url, sex, pic_small from user where uid={0}', response.id);
query.wait(function(rows) {
document.getElementById('debug').innerHTML =
'FQL Information: '+ "<br />" +
'Your name: ' + rows[0].name
+ "<br />" +
'Your Sex: ' + (rows[0].sex!= undefined ? rows[0].sex : "") + "<br />" +
'Your Profile: ' + "<a href='" + rows[0].profile_url + "'>" + rows[0].profile_url + "</a>" + "<br />" +
'<img src="' + rows[0].pic_small + '" alt="" />' + "<br />";
});
});
}
function setStatus(){
showLoader(true);
status1 = document.getElementById('status').value;
FB.api(
{
method: 'status.set',
status: status1
},
function(response) {
if (response == 0){
alert('Your facebook status not updated. Give Status Update Permission.');
}
else{
alert('Your facebook status updated');
}
showLoader(false);
}
);
}
function showLoader(status){
if (status)
document.getElementById('loader').style.display = 'block';
else
document.getElementById('loader').style.display = 'none';
}
</script>
<button id="fb-auth">Login</button>
<div id="loader" style="display:none">
<img src="ajax-loader.gif" alt="loading" />
</div>
<br/>
<div id="user-info"></div>
<br/>
<div id="debug"></div>
<div id="other" style="display:none">
<a href="#" onclick="showStream(); return false;">Publish Wall Post</a> |
<a href="#" onclick="share(); return false;">Share With Your Friends</a> |
<a href="#" onclick="graphStreamPublish(); return false;">Publish Stream Using Graph API</a> |
<a href="#" onclick="fqlQuery(); return false;">FQL Query Example</a>
<br/>
<textarea id="status" cols="50" rows="5">hello muffin hello muffin</textarea>
<br/>
<a href="#" onclick="setStatus(); return false;">Status Set Using Legacy Api Call</a>
</div>
var按钮;
var用户信息;
window.fbAsyninit=函数(){
init({appId:'您的appId',//将appId更改为您的appId
状态:正确,
曲奇:是的,
xfbml:是的,
oauth:true});
//showLoader(真);
函数更新按钮(响应){
button=document.getElementById('fb-auth');
userInfo=document.getElementById('user-info');
if(response.authResponse){
//用户已登录并连接
FB.api('/me',函数(info){
登录(响应、信息);
//警报(信息邮件);
});
button.onclick=函数(){
FB.注销(功能(响应){
注销(响应);
});
};
}否则{
//用户未连接到您的应用或未注销
button.innerHTML='Login';
button.onclick=函数(){
FB.登录(功能(响应){
if(response.authResponse){
FB.api('/me',函数(info){
登录(响应、信息);
});
}
其他的
{
//用户已取消登录或未授予授权
showLoader(假);
}
},{范围:'email,user_birth,status_update,publish_stream,user_about_me'});
}
}
}
//使用当前状态运行一次,并在状态更改时运行一次
FB.getLoginStatus(更新按钮);
FB.Event.subscribe('auth.statusChange',updateButton);
};
(功能(){
var e=document.createElement('script');e.async=true;
e、 src=document.location.protocol
+“//connect.facebook.net/en_US/all.js”;
document.getElementById('fb-root').appendChild(e);
}());
函数登录(响应、信息){
if(response.authResponse){
var accessToken=response.authResponse.accessToken;
userInfo.innerHTML=''+info.name;
button.innerHTML='Logout';
showLoader(假);
document.getElementById('other').style.display=“block”;
}
}
功能注销(响应){
userInfo.innerHTML=“”;
document.getElementById('debug').innerHTML=“”;
document.getElementById('other').style.display=“无”;
showLoader(假);
}
//流发布方法
函数streamPublish(名称、描述、hrefTitle、hrefLink、userPrompt){
showLoader(真);
FB.ui(
{
方法:“stream.publish”,
消息:“”,
附件:{
姓名:姓名,,
标题:'',
描述:(描述),
href:hrefLink
},
行动链接:[
{文本:hrefTitle,href:hrefLink}
],
用户提示消息:用户提示
},
功能(响应){
showLoader(假);
});
}
函数showStream(){
FB.api('/me',函数(响应){
//console.log(response.id);
streamPublish(response.name,'我喜欢muffin.my的文章','hrefTitle','http://muffin.my“分享松饼,我的”);
});
}
函数共享(){
showLoader(真);
风险值份额={
方法:“stream.share”,
u:'http://muffin.my/'
};
FB.ui(共享、功能(响应){
showLoader(假);
控制台日志(响应);
});
}
函数图StreamPublish(){
showLoader(真);
FB.api(“/me/feed”、“post”,
{
信息:“我爱松饼。我的松饼松饼”,
链接:'http://muffin.my',
图片:'http://muffin.my/iphone/lucky7_ios.jpg',
名称:“iOS应用程序和游戏”,
描述:“松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼松饼”
},
功能(响应){
showLoader(假);
如果(!response | | response.error){
警报(“发生错误”);
}否则{
警报('Post ID:'+response.ID);
}
});
}
函数fqlQuery(){
showLoader(真);
FB.api('/me',函数(响应){
showLoader(假);
//http://developers.facebook.com/docs/reference/fql/user/
var query=FB.Data.query('select name,profile\u url,sex,pic\u small from user where uid={0}',response.id);
query.wait(函数(行){
document.getElementById('debug')。innerHTML=
'FQL信息:'+“
”+
“您的姓名:”+行[0]。姓名
+“
”+
“您的性别:”+(行[0]。性别!=未定义?行[0]。性别:)+“
”+
'您的个人资料:'+“”+“
”+
''+“
”;
});
});
}
函数setStatus(){
showLoader(真);
status1=document.getElementById('status')。值;
FB.api(
{
方法:“status.set”,
状态:状态1
},
功能(响应){
如果(响应)
$query = (new FacebookRequest(
$facebookSession, 'GET', 'me?fields=id,name'
))->execute()->getGraphObject(GraphUser::className())->asArray();
$id = $query['id'];