0

我正在使用 SDK php 让用户使用他们的 Facebook 帐户登录我的网站。我正在关注 SDK 示例。当我试图获取登录的用户 ID 时,它返回 0:

$id = $facebook->getUser();

你能告诉我有什么问题吗,以及如何使用 php SDK 使示例工作

这是示例的代码:

<?php
$config = FacebookConfig::getConfig();
$facebook = new Facebook( $config );
$user = $facebook->getUser();
Facebook::$CURL_OPTS[ CURLOPT_SSL_VERIFYPEER ] = false;

if ( $user ) {
    try {
        $user_profile = $facebook->api( '/me' );
        $logoutUrl = $facebook->getLogoutUrl();
    }
    catch ( FacebookApiException $e ) {
        error_log( $e );
        $user = null;
    }
} else {
    try {

        $loginUrl = $facebook->getLoginUrl( array(
             "scope" => "email,user_education_history,user_work_history",
            "redirect_uri" => "http://localhost:8888/www/index.php" 
        ) );
        $user = $facebook->getUser();
        $user_profile = $facebook->api( '/me' );
        $userProf = $facebook->api( "/$user" );
        echo $user;
        echo $user_profile;
    }
    catch ( FacebookApiException $e ) {
        $user = null;
        echo "expetion in login";

    }
}
?>
4

1 回答 1

0

您必须将用户重定向到$loginUrl!尝试这个:

<?php
$config = FacebookConfig::getConfig();
$facebook = new Facebook( $config );
$user = $facebook->getUser();
Facebook::$CURL_OPTS[ CURLOPT_SSL_VERIFYPEER ] = false;

if ( $user ) {
    try {
        $user_profile = $facebook->api( '/me' );
        var_dump($user_profile);
        $logoutUrl = $facebook->getLogoutUrl();
    }
    catch ( FacebookApiException $e ) {
        error_log( $e );
        $user = null;
    }
} else {
    try {
        $loginUrl = $facebook->getLoginUrl( array(
             "scope" => "email,user_education_history,user_work_history",
            "redirect_uri" => "http://localhost/facebook/index.php" 
        ) );
        header('Location: '.$loginUrl);
    }
    catch ( FacebookApiException $e ) {
        $user = null;
        echo "exception in login";
    }
}
于 2013-03-05T15:35:19.023 回答