Я могу войти с моим приложением facebook, но сеанс, который я получаю, всегда NULL. Вот мой весь PHP-код:
<?php session_start(); //Session should always be active $app_id = 'mySexyAppID'; //DEV Name: PHP_SDK-Prev-FB\0.0.2b4 $app_secret = 'someS3cr3tStuff'; $required_scope = 'public_profile, publish_actions,publish_stream,manage_pages,user_groups'; //Permissions required $redirect_url = 'http://localhost/fb/facebook-sdk-v4-sample-files/index2.php'; //FB redirects to this page with a code require_once __DIR__ . "/facebook-php-sdk-v4-4.0-dev/autoload.php"; //include autoload from SDK folder //define output vars $responseMembers_String = '0'; //import required class to the current scope use Facebook\FacebookSession; use Facebook\FacebookRequest; use Facebook\GraphUser; use Facebook\FacebookRedirectLoginHelper; FacebookSession::setDefaultApplication($app_id , $app_secret); $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 ) { // catch any exceptions $session = null; } } if ( !isset( $session ) || $session === null ) { // no session exists try { $session = $helper->getSessionFromRedirect(); $_SESSION["FBSession"] = $session; } catch( FacebookRequestException $ex ) { print_r( $ex->getMessage()); } catch( Exception $ex ) { print_r( $ex->getMessage()); } } // see if we have a session if ( isset( $session ) ) { $_SESSION["SessionSetText"] = "Session set with success!"; } /**************************************************************/ //if user wants to log out if(isset($_GET["log-out"]) && $_GET["log-out"]==1){ unset($_SESSION["fb_user_details"]); //session ver is set, redirect user header("location: ". $redirect_url); } //Test normal login / logout with session if ($session){ //if we have the FB session //get user data $user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className()); $user_accounts = (new FacebookRequest($session, 'GET', '/me/accounts'))->execute()->getGraphObject(GraphUser::className()); $user_groups = (new FacebookRequest($session, 'GET', '/me/groups'))->execute()->getGraphObject(GraphUser::className()); //save session var as array $_SESSION["fb_user_details"] = $user_profile->asArray(); $_SESSION["accounts"] = $user_accounts->asArray(); $_SESSION["groups"] = $user_groups->asArray(); $user_id = ( isset( $_SESSION["fb_user_details"]["id"] ) )? $_SESSION["fb_user_details"]["id"] : ""; $user_name = ( isset( $_SESSION["fb_user_details"]["name"] ) )? $_SESSION["fb_user_details"]["name"] : ""; $user_email = ( isset( $_SESSION["fb_user_details"]["email"] ) )? $_SESSION["fb_user_details"]["email"] : ""; //session var is set, redirect user header("location: ". $redirect_url); } if(isset($_POST["group_submit"])) { //user wants informations about a group where he is administrator $userGroupId = $_POST["usergroups"]; //$group_url = '/'.$userGroupId.'/members'; //request members via facebook graph api /**************************************************/ $requestMembers = new FacebookRequest($session, 'GET', '/'.$userGroupId.'/members'); $requestMembers_response = $requestMembers->execute()->getGraphObject()->asArray(); /**************************************************/ $responseMembers_String = $requestMembers_response; $_SESSION["GroupMembers"] = $responseMembers_String; } ?> <html> <head> <meta charset="utf-8" /> <title>Demo StackOverflow</title> </head> <body> <form id="form" name="form" method="POST" action="index2.php"> <?php if(isset($_SESSION["fb_user_details"])) { print 'Hi '.$_SESSION["fb_user_details"]["name"].' you are logged in! [ <a href="?log-out=1">log-out</a> ] '; } else{ //display login url $login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) ); echo '<a href="'.$login_url.'">Connect with Facebook</a>'; }; ?> <br /> <br /> <?php if(isset($_SESSION["fb_user_details"])) { print 'Some infos about the current user ['.$_SESSION["fb_user_details"]["first_name"].']: <pre>'; print_r($_SESSION["fb_user_details"]); print '</pre>'; }; ?> <br /> <select name="usergroups" id="ugroups"> <?php foreach ($_SESSION["groups"]["data"] as $group) { //only fetch groups where user is admin! if($group->administrator) { echo '<option value="'.$group->id.'">'.$group->name.'</option>'; } } ; ?> </select> <br /> <button type="submit" class="button" id="group_submit" name="group_submit">Get some informations!</button><br /> <label id="GroupMembers" name="GroupMembers"><?php if (!(isset($_SESSION["GroupMembers"]))){print 'not set now!';} elseif(isset($_SESSION["GroupMembers"])) {print 'the selected group has '.$responseMembers_String.' Members';} ?></label><br /> <label id="lInfos" name="lInfos"><?php if (!(isset($_SESSION["FBSession"]))){print '$session not set!!';} elseif(isset($_SESSION["FBSession"])) {print 'Session set!';} ?></label> </form> </body> </html>
Когда я вызываю функцию графа (запрос членов группы: $requestMembers = new FacebookRequest($session, 'GET', '/'.$userSelectedGroupID.'/members');
) Я получаю следующую ошибку:
Catchable fatal error: Argument 1 passed to Facebook\FacebookRequest::__construct() must be an instance of Facebook\FacebookSession, null given
Я обновил вопрос и добавил полный пример со всем кодом. Я понял, что у меня сеанс где угодно, потому что я могу выбрать группы пользователей.