• I want to develop voting plugin for WordPress. Votes can be given using only facebook login. My folder structure as follow,

    plugin-
     |-admin
     |-includes
     |---Facebook/
     |---autoload.php
     |---loadsdk.php
     |-plugin.php

    I tried, include( MY_PLUGIN_PATH . 'includes/loadsdk.php'); in plugin.php.

    <?php
    
    session_start();
    
    define("FB_APP_ID", "345345345345345");
    define("FB_APP_SECRET", "dsfgdsfgsdfgsdfg");
    define("REDIRECT_URL", "https://myweb.com/folder/");
    define("FB_PERMISSIONS",'email');
    
    require 'autoload.php';
    
    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    
    use Facebook\GraphNodes\GraphUser;
    use Facebook\Entities\SignedRequest;
    use Facebook\Exceptions\FacebookRequestException;
    use Facebook\Helpers\FacebookRedirectLoginHelper;
    
    // init app with app id and secret
    FacebookSession::setDefaultApplication(FB_APP_ID, FB_APP_SECRET);
    
    // login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper(REDIRECT_URL);
    
    // see if a existing session exists
    if (isset($_SESSION) && isset($_SESSION['fb_token'])) {
    	// create new session from saved access_token
    	$session = new FacebookSession($_SESSION['fb_token']);
    
    	// validate the access_token to make sure it's still valid
    	try {
    		if (!$session -> validate()) {
    			$session = null;
    		}
    	} catch ( Exception $e ) {
    		$session = null;
    	}
    }
    
    if (!isset($session) || $session === null) {
    	// no session exists
    	try {
    		$session = $helper -> getSessionFromRedirect();
    	} catch( FacebookRequestException $ex ) {
    	} catch( Exception $ex ) {
    	}
    }
    
    // see if we have a session
    
    if (isset($session)) {
    	// save the session
    	$_SESSION['fb_token'] = $session -> getToken();
    
    	// create a session using saved token or the new one we generated at login
    	$session = new FacebookSession($session -> getToken());
    
    	// graph api request for user data
    	$request = new FacebookRequest($session, 'GET', '/me?fields=first_name,last_name,email');
    	$response = $request -> execute();
    
    	// get response
    	$graphObject = $response -> getGraphObject() -> asArray();
    
    	print_r($graphObject);
    
    	// print logout url using session and redirect_uri (logout.php page should destroy the session)
    	$logoutURL = $helper -> getLogoutUrl($session, CANVAS);
    
    } else {
    
    	// show login url
    	echo $loginURL = '<a href="' . $helper -> getLoginUrl(array(FB_PERMISSIONS)) . '">Login with facebook</a>';
    
    }
    ?>

    I can see bottom login-url on web site.though i click on it redirect with code as response. I think session is not creating.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use Facebook PHP SDK v5.0 in wordpress plugin?’ is closed to new replies.