2014-09-10 30 views
0

我能夠獲取除用戶的用戶名(我需要在我的網站上(在CI Framework中編碼))中的所有用戶的詳細信息。我希望我的網站能夠與Facebook完全整合。 我已經使用PHP的SDK代碼。 下面我提供了完整的代碼。請讓我知道,如果我可以使用用戶的fb id和FQL獲取用戶的用戶名。無法使用PHP SDK獲取用戶的用戶名

<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> 

回答

0

我沒有在您的代碼中看到任何php,只有javascript。

Facebook現在正在使用多個API。

使用最新版本( '2.1')

 $query = (new FacebookRequest(
              $facebookSession, 'GET', 'me?fields=id,name' 
              ))->execute()->getGraphObject(GraphUser::className())->asArray(); 
        $id = $query['id']; 
相關問題