在HumHub中使用第三方应用程序获取用户详细信息

时间:2015-07-05 12:00:37

标签: php yii social-networking

我正在为humhub构建自定义网络研讨会。我正在使用Custom_pages模块(this) 如何在我的应用程序中获取当前登录用户的用户详细信息?

有人给了我这个代码试试

//plug in to Yii application
require_once('../protected/vendors/yii/yii.php');
Yii::createWebApplication('../protected/config/main.php');

//------------------

//print_r(Yii::app());

echo "<br />";

//check if user is logged in (not a guest)
if(!Yii::app()->user->isGuest)
{
    //if logged in, display the username and ID
    echo "<strong>Logged In.</strong>";
    echo "<br />";
    echo "Username: ".Yii::app()->user->name;
    echo "<br />";
    echo "User ID: ".Yii::app()->user->id;
}
else
{
    //if not logged in, display username (Guest)
    echo "<strong>Not Logged In.</strong>";
    echo "<br />";
    echo "Username: ".Yii::app()->user->name;
}

但由于某种原因,line 3会导致页面无法加载。如果我将其评论出来,页面会加载,但Yii为空。

有什么方法可以在我的第三方应用中获取登录用户的用户详细信息吗?

任何方法都将受到赞赏

2 个答案:

答案 0 :(得分:1)

我使用了你的代码,它对我很好,但我需要根据我的需要修改它。也许我的修改对你有用。

    <?PHP
//this script gets the id and email "as name" from hum hub 
require_once('../humhub-master/protected/vendors/yii/yii.php');
Yii::createWebApplication('../humhub-master/protected/config/main.php');

//I create these vars to use in my custom pages
    $hum_uid=Yii::app()->user->id;
    $hum_name=Yii::app()->user->name;
    // this should get me a real username
$servername = "******";
$username = "******";
$password = "*****";
$dbname = "humhub";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, username FROM user WHERE id='$hum_uid'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $hum_username =  $row["username"];
        echo $hum_username;
    }
} else {
    echo "0 results";
}
?>

我把它放在humhub的iframe自定义页面的顶部。当您需要用户名时,只需使用$ hum_username。

使用sql select,您可以从数据库中的用户表中获取任何您想要的内容WHERE id =&#39; $ hum_uid&#39;

答案 1 :(得分:0)

更容易:

$userUsername = Yii::$app->user->identity->username;

其他有用的变种:

$userDisplayName = Yii::$app->user->identity->getDisplayName();
$userAttributesArray = Yii::$app->user->identity->getSearchAttributes(); // all profile fields and groups      
$userIsGuest = Yii::$app->user->isGuest; // true = loggedOut, false = loggedIn
$userIsAdmin = \humhub\modules\admin\widgets\AdminMenu::canAccess();