简体   繁体   English

使用wp-rest api登录wordpress网站

[英]Login wordpress website using wp-rest api

I'm using Wordpress 4.8 and i am new to rest api . 我使用的是Wordpress 4.8 ,我是REST API的新手。 I have 2 websites , one is www.example.com and second is www.example.com/site2 . 我有2个网站,一个是www.example.com ,第二个是www.example.com/site2 That is second website is installed inside the first . 那就是第二个网站安装在第一个里面。

In example.com I have many users. 在example.com中,我有很多用户。 In site 2 , i have a custom page & in that page i create a custom forms for login . 在站点2中,我有一个自定义页面,并在该页面中创建了一个用于登录的自定义表单。 The form is checking the details based on first website users . 该表格正在根据第一个网站用户检查详细信息。 That is some one enter the login details of first website then they can able to login . 即有人输入第一个网站的登录详细信息,然后他们便可以登录。 Please check my form 请检查我的表格

<form>
<input type="text" name="user_name" class="user_name" required>
<input type="password" name="user_password" class="user_password" required>
<input tuype="submit" name="submit" class="submit">
</form>

<script>

     $(".submit").on("click",function(){
           var username = $(".user_name").val();
           var password = $(".user_password").val();
            });

</script>

Please help to complete this form .Should i need to use curl ? 请帮助填写此表格。我需要使用curl吗? How can i check the login details with the help of rest api 我如何在Rest API的帮助下检查登录详细信息

Edit 编辑

From my further investigation i am able to do this much code 根据我的进一步调查,我能够完成这么多代码

   $(".submit").on("click",function(){
       var username = $(".user_name").val();
       var password = $(".user_password").val();
       var posting = $.post( 'ajax.php', { user1: username, pass1:password } );
           posting.done(function( data ) {
             MYCODE 

             });
    });

In ajax.php I write 我在ajax.php中编写

 ini_set('display_errors', 0);
    error_reporting(E_ALL);

$username = $_POST["user1"];
$password = $_POST["pass1"];
$headers = array();
//$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/json';

$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL,"http://www.example.com/wp-json/");
curl_setopt($curl, CURLOPT_HEADER ,0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('username'=>$username, 'password'=>$password));
$resp = curl_exec($curl);

curl_close($curl);

echo $resp;

Still no luck. 仍然没有运气。

There is many way to login , 有很多登录方式,
1. cookie based authentication 1.基于cookie的身份验证
2. OAuth used 2.使用的OAuth
3. Token based Auth 3.基于令牌的身份验证

you can use below plugin for token based Auth , https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/ 您可以将以下插件用于基于令牌的Auth, https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM