将API身份验证集成到WordPress

时间:2016-11-25 13:31:10

标签: wordpress api

我有一个网站,我必须对在另一个系统中注册的用户进行身份验证(在本例中为Kayako支持系统)。 我想我必须使用API​​来解决这个问题,但我真的不知道如何开始。

有人可以帮我解决这个问题吗?如何发送身份验证所需的数据以及如何管理从Kayako获得的响应。

1 个答案:

答案 0 :(得分:1)

弄清楚Kayako系统的API是怎样的。在WordPress中,您可以执行类似这样的操作来验证用户:

// this action is executed just before the invocation of the WordPress authentication process
add_action('wp_authenticate','checkTheUserAuthentication');

function checkTheUserAuthentication() {

     $username=$_POST['log'];
     $password=$_POST['pwd'];

    // try to log into the external service or database with username and password
    $ext_auth = try2AuthenticateExternalService($username,$password);

    // if external authentication was successful
    if($ext_auth) {

         // find a way to get the user id
         $user_id = username_exists($username);
         // userdata will contain all information about the user
         $userdata = get_userdata($user_id);
         $user = set_current_user($user_id,$username);

         // this will actually make the user authenticated as soon as the cookie is in the browser
         wp_set_auth_cookie($user_id);
         // the wp_login action is used by a lot of plugins, just decide if you need it
        do_action('wp_login',$userdata->ID);

        // you can redirect the authenticated user to the "logged-in-page", define('MY_PROFILE_PAGE',1); f.e. first
        header("Location:".get_page_link(MY_PROFILE_PAGE));
    }

}

try2AuthenticateExternalService()方法应该包含一些远程服务的curl-request(或类似)。

相关问题