无法获得facebook Session

时间:2016-08-09 08:10:31

标签: php wordpress facebook woocommerce

我正在Woocommerce文件 - abstract-wc-payment-gateway.php上获得Facebook会话。但是当我使用get_user_id_awpg()函数获取Facebook user id时,函数返回null。什么都不打印出来。

但是,我可以在其他文件上使用此功能,不知道是什么问题。

这是我的代码:

<?php 


 if ( ! defined( 'ABSPATH' ) ) {
    exit;
 }

 require_once __DIR__ . '/src/Facebook/autoload.php';

 abstract class WC_Payment_Gateway extends WC_Settings_API {
      public  function get_user_id_awpg()
       {
           if(!isset($_SESSION)){
                session_start();
            }
                  $fb = new Facebook\Facebook
                  ([
                             'app_id' => 'app_id',  //Your apps Id
                            'app_secret' => 'app_secret',
                            'default_graph_version' => 'v2.6',
]);

$helper = $fb->getRedirectLoginHelper();

    $permissions = ['email', 'user_friends','user_birthday']; // optional

    try {
        if (isset($_SESSION['facebook_access_token'])) {
            $accessToken = $_SESSION['facebook_access_token'];

        } else {
            $accessToken = $helper->getAccessToken();
        }
    } catch(Facebook\Exceptions\FacebookResponseException $e) {

    // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error 0: ' . $e->getMessage();
        exit;
    }

    if (isset($accessToken)) {

        if (isset($_SESSION['facebook_access_token'])) {

            $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);

        } else {
        // getting short-lived access token
            $_SESSION['facebook_access_token'] = (string) $accessToken;

            // OAuth 2.0 client handler
            $oAuth2Client = $fb->getOAuth2Client();

            // Exchanges a short-lived access token for a long-lived one
            $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);

            $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

            // setting default access token to be used in script
            $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        }

    // redirect the user back to the same page if it has "code" GET variable
        if (isset($_GET['code'])) {
            header('Location: ./');
        }

        // getting basic info about user
        try {

            $profile_request = $fb->get('/me?fields=name, age_range{max}, education{school{name}}');
            $profile = $profile_request->getGraphNode()->asArray();
                                                                                     $user_id = $profile['id'];
        } catch(Facebook\Exceptions\FacebookResponseException $e) {

            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            session_destroy();

            // redirecting user back to app login page
            header("Location: ./");
            exit;

        } catch(Facebook\Exceptions\FacebookSDKException $e) {

            // When validation fails or other local issues
            echo 'Facebook SDK returned an error 1: ' . $e-> getMessage();
            exit;

        }


            }

                  return $user_id;
       }

很多。

0 个答案:

没有答案