FB Connect:显示response.name

时间:2013-10-30 03:13:00

标签: javascript php login connect

我想使用php回显FB用户的response.name,但它无效。我也提醒它,结果是“Undefined”

<div id="fb-root"></div> <script>
  window.fbAsyncInit = function() {
  FB.init({
    appId      : 'xxxxxxxxx',
    channelUrl : 'www.xxxxxxxxx.com',
    status     : true,
    cookie     : true,
    xfbml      : true
  });
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    if (response.status === 'connected') {
      alert(getUserInfo());
    } else if (response.status === 'not_authorized') {
      FB.login();
    } else {
      FB.login();
    }
  });
  };
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));
  function getUserInfo() {
   var str='';
        FB.api('/me', function(response) {
       str=response.name;
    });
    return str
    }
</script>
<fb:login-button show-faces="false" width="200" max-rows="1" autologoutlink='true'></fb:login-button>

我想获取response.name然后将其插入数据库。我想,我需要知道的是如何把它放在php的变量中?或者把它放在javascript变量中然后发送到php文件。

1 个答案:

答案 0 :(得分:0)

function getUserInfo() {
   var str='';
        FB.api('/me', function(response) {
       str=response.name;

       //Do ajax post to php here
    });

}