简体   繁体   中英

How do I get require_login()-like functionality using the new PHP Client Library for Facebook?

Howdy. I've been tasked with making a Facebook game, but I'm new to Facebook development, so I'm just getting started. Apologies in advance if this is a no-brainer to people.

I'm having trouble following all the examples I see on sites, and I keep running into missing pages in the Facebook documentation when I am trying to read up. I think it's because there's a new version of the PHP Client Library for Facebook, and everything I'm finding is referring to the old client.

For instance, I see this code in a lot of examples:

require 'facebook.php';
$facebook = new Facebook( array( 'appId' => '(id)', 'secret' => '(secret)' ) );
$facebook_account = $facebook->require_login();

...but there's no "require_login()" in the client library provided in the facebook.php file.

From what I can tell, it looks like Facebook has very recently rolled out some new system for development, but I don't see any sample code anywhere to deal with it. The new library comes with an "example.php" file, but it appears to be only for adding "Log in with Facebook" functionality to other sites (what I'm assuming is what they mean by "Facebook Connect" sites), not for just running apps in a Canvas page on Facebook itself.

Specifically, what I need to do is let users visit an application page within Facebook, have it bring up the dialog box allowing them to authorize the app, have it show up in their "games" page, and then have it pass me the relevant info about the user so I can start creating the game. But I can't seem to find any tutorials or examples that show how to do this using the new library. Seems like this should be pretty straightforward, but I'm running into roadblocks.

Or am I missing something about the PHP client library? Should require_login() be working for me, and there's something broken with my implementation, such as having the wrong client library or something? I downloaded from GitHub yesterday, so I'm pretty sure I have the most recent version of the code I have, but perhaps I'm downloading the wrong "facebook.php" file...?

The following is a rewrite of the old require_login function. It exactly duplicates the old functionality.

function facebook_require_login($required_permissions = '')
{
  global $facebook; // NOTE GLOBAL FACEBOOK OBJECT, MUST ALREADY BE INSTANTIATED

  $user = $facebook->get_loggedin_user();
  $has_permissions = true;

  if ($required_permissions) {
    $facebook->require_frame();
    $permissions = array_map('trim', explode(',', $required_permissions));
    foreach ($permissions as $permission) {
      if (!in_array($permission, $facebook->ext_perms)) {
        $has_permissions = false;
        break;
      }
    }
  }

  if ($user && $has_permissions) return $user;

  $facebook->redirect(
    $facebook->get_login_url(Facebook::current_url(), $facebook->in_frame(),
                         $required_permissions));
}

phpfour solution is the only correct one - since it utilizes the new php-sdk library from github.

The best solution is to edit the new facebook.php and add a require_login() function (so all existing pages who rely on it can stay the same)

public function require_login(){
  if ( !$this->getSession() ) {
    $url = $this->getLoginUrl( array(
       'canvas' => 1,
       'fbconnect' => 0
     ));
    echo "<script type='text/javascript'>top.location.href = '$url';</script>";
  }
 else
  return $this->getUser();
}

The new php script on git hub is a wraper for facebooks api, graph I presume but I've seen code for fql too so who knows. The forums are currently down and IRC was dead when I went across. I have been looking for the same solution as your require authorisation to call ->api(\\me). Since the script extends another class if I remember right, you could try using the reflection class/function to see what methods are available. Hopefully some solid documentation is on its way! Please let me know if you solve this. (Sorry for poor formatting I'm on my mobile)

Well, I have been able to find a solution to the problem of authorizing application using the new PHP SDK. You can check my blog post here .

In short, you will need to get an authenticated session and then you can call the functions to get the logged in user's ID. In this case, you will call the "/me" path from the Graph API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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