PHP facebook图形api用于获取用户' s-city,state,zip

时间:2015-06-29 02:07:52

标签: php facebook facebook-graph-api facebook-oauth

我尝试登录Facebook,可以呈现用户的城市,州,邮政编码。

以下是代码,任何人都可以告诉我哪里出错了。

我正在使用Facebook图形api(PHP SDK V4.0)

 <?php

    require_once( 'lib/Facebook/FacebookSession.php');
    require_once( 'lib/Facebook/FacebookRequest.php' );
    require_once( 'lib/Facebook/FacebookResponse.php' );
    require_once( 'lib/Facebook/FacebookSDKException.php' );
    require_once( 'lib/Facebook/FacebookRequestException.php' );
    require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
    require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
    require_once( 'lib/Facebook/GraphObject.php' );
    require_once( 'lib/Facebook/GraphUser.php' );
    require_once( 'lib/Facebook/GraphSessionInfo.php' );
    require_once( 'lib/Facebook/Entities/AccessToken.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
    require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;
    use Facebook\FacebookHttpable;
    use Facebook\FacebookCurlHttpClient;
    use Facebook\FacebookCurl;


/*PROCESS*/

    //1.Stat Session
     session_start();
    //check if users wants to logout
     if(isset($_REQUEST['logout'])){
        unset($_SESSION['fb_token']);
     }

    //2.Use app id,secret and redirect url 
    $app_id = '';
    $app_secret = '';
    $redirect_url='http://demo.domain.com/apps/fblogin/';
    //3.Initialize application, create helper object and get fb sess
     FacebookSession::setDefaultApplication($app_id,$app_secret);
     $helper = new FacebookRedirectLoginHelper($redirect_url);
     $sess = $helper->getSessionFromRedirect();
    //check if facebook session exists
    if(isset($_SESSION['fb_token'])){
        $sess = new FacebookSession($_SESSION['fb_token']);
    }
    //logout
    $logout = 'http://demo.domain.com/apps/fblogin?logout=true';
    //4. if fb sess exists echo name 
        if(isset($sess)){
            //store the token in the php session
            $_SESSION['fb_token']=$sess->getToken();
            //create request object,execute and capture response
            $request = new FacebookRequest($sess,'GET','/me');
            // from response get graph object
            $response = $request->execute();
            $graph = $response->getGraphObject(GraphUser::classname());
            // use graph object methods to get user details
            $name = $graph->getName();
            $id = $graph->getId();
            $image = 'https://graph.facebook.com/'.$id.'/picture?width=100';
            $email = $graph->getProperty('email');
            $gender = $graph->getProperty('gender');
            $city = $this->getProperty('city');
            //$state =  $this->getProperty('state');
            //$country = $graph->getProperty('country');
            //$zip = $this->getProperty('zip');


            if (isset($graph->getProperty['phone'])){
                $phone = $graph->getProperty['phone'];
            }
            /*------------------------------------------*/
            echo "Your Name is $name <br><Br>";
            echo "Your Email is $email <br><Br>";
            echo "Your Gender is $gender <br><Br>";
            echo "Your Phone is $phone <br><Br>";
            echo "<img src='$image' /><br><br>";
            echo "<a href='".$logout."'><button>Logout</button></a>";
            echo "Your City is $city <br><Br>";
            //echo "Your State is $state <br><Br>";
            //echo "Your Country is $country <br><Br>";
            //echo "Your Zip is $zip <br><Br>";
           /*------------------------------------------*/

        }else{
            //else echo login
            echo '<a href="'.$helper->getLoginUrl(array('email')).'" >Login with facebook</a>';
        }

登录后我的空白屏幕......

1 个答案:

答案 0 :(得分:1)

用户表中没有statecitycountryhttps://developers.facebook.com/docs/graph-api/reference/v2.3/user

location字段可能是最接近的字段。如果你得到一个空白页面,你应该检查你的错误日志。