简体   繁体   English

Facebook Connect-同时使用FBML和PHP客户端

[英]Facebook Connect - Using both FBML and PHP Client

I am doing my second Facebook connect site. 我正在做我的第二个Facebook连接站点。 On my first site, I used the facebook coonect FBML to sign a user in, then I could access other information via the PHP Client. 在我的第一个站点上,我使用facebook coonect FBML登录用户,然后可以通过PHP客户端访问其他信息。 With this site, using the same exact code, it doesn't seem to be working. 在此站点上,使用完全相同的代码似乎不起作用。 I'd like someone to be able to sign in using the Facebook button: 我希望有人能够使用Facebook按钮登录:

<fb:login-button length="long" size="medium" onlogin="facebook_onlogin();"></fb:login-button>

After they have logged in, the facebook_onlogin() function makes some AJAX requests to a PHP page which attempts to use the Facebook PHP Client. 登录后,facebook_onlogin()函数会向尝试使用Facebook PHP客户端的PHP页面发出一些AJAX请求。 Here's the code from that page: 这是该页面上的代码:

    $facebook = new Facebook('xxxxx API KEY xxxxx', 'xxxxx SECRET KEY xxxxx');
    //$facebook->require_login(); ** I DONT WANT THIS LINE!!

    $userID = $facebook->api_client->users_getLoggedInUser();

    if($userID) {

        $user_details = $facebook->api_client->users_getInfo($userID, 'last_name, first_name'); 
        $firstName = $user_details[0]['first_name']; 
        $lastName = $user_details[0]['last_name']; 

        $this->view->userID = $userID;
        $this->view->firstName = $firstName;
        $this->view->lastName = $lastName;
    }

The problem is that the PHP Client thinks I don't have a session key, so the $userID never get set. 问题在于PHP客户端认为我没有会话密钥,因此从未设置$ userID。 If I override the $userID with my facebook ID (which I am logged in with) I get this error: 如果我用我的Facebook ID(使用我的登录名)覆盖了$ userID,则会收到此错误:

exception 'FacebookRestClientException' with message 'A session key is required for calling this method' in C:\\wamp\\www\\mab\\application\\Facebook\\facebookapi_php5_restlib.php:3003 C:\\ wamp \\ www \\ mab \\ application \\ Facebook \\ facebookapi_php5_restlib.php:3003中带有消息“调用此方法需要会话密钥”的异常“ FacebookRestClientException”

If I uncomment the require_login(), it will get a session ID, but it redirects pages around a lot and breaks my AJAX calls. 如果我取消注释require_login(),它将获得一个会话ID,但是它将重定向页面很多,并中断了我的AJAX调用。 I am using the same code I successfully did this with on another site. 我使用的代码与在其他站点成功完成的代码相同。 Anyone have any ideas why the PHP client don't know about the session ID after a user has logged in with the connect button? 任何人都有任何想法,为什么在用户使用connect按钮登录后,PHP客户端不知道会话ID? Any ideas would be greatly appreciated. 任何想法将不胜感激。 Thanks! 谢谢!

You can extract the user id without a API call, directly from the cookies, by using: 您可以使用以下方法直接从cookie中提取无需API调用的用户ID:

$facebook->get_loggedin_user();

Note that this is on the main $facebook object, not the api_client object it contains. 请注意,这是在主要的$facebook对象上, 而不是它包含的api_client对象。

Never try to use facebook connect and the PHP client from localhost.... that's the answer. 永远不要尝试使用facebook连接和来自本地主机的PHP客户端。 You can get it to work but it causes several 'hidden' issues that you don't have to deal with when working from a public domain. 您可以使其正常工作,但是会导致一些“隐患”问题,而这些问题在从公共领域工作时不必处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM