简体   繁体   English

您可以使用 curl 向 wordpress 发送登录帖子请求吗?

[英]Can you use curl to send a login post request to wordpress?

I am currently trying to set something up that allows me to sort of remote log into wordpress, im not sure if im going about it right or if it's possible but it seems to be from a few things i've read.我目前正在尝试设置一些东西,让我可以远程登录 wordpress,我不确定我是否正确或是否可能,但它似乎来自我读过的一些东西。

Im using the code below:我使用下面的代码:

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://example.com/wp-login.php',
    CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'log' => 'username',
        'pwd' => 'password',
        'wp-submit' => 'Log In',
        'redirect_to' => 'http://example.com/wp-admin/',
        'testcookie' => 1
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
die(var_dump($resp));

// Close request to clear up some resources
curl_close($curl);

When i die var_dump it the page returns this and nothing else: string(0) "" that is all.当我死 var_dump 时,页面返回这个,没有别的: string(0) ""就是这样。

but when i remove the "pwd" field from the curl request it returns the login page along with an error message stating that the password from the password field is missing但是当我从 curl 请求中删除“pwd”字段时,它会返回登录页面以及一条错误消息,指出密码字段中的密码丢失

EDIT编辑

New error on successful login:成功登录时出现新错误:

ERROR: Cookies are blocked or not supported by your browser.错误:您的浏览器阻止或不支持 Cookie。 You must enable cookies to use WordPress.您必须启用 cookie 才能使用 WordPress。

I have no idea what this is or why its doing it我不知道这是什么或为什么这样做

I think WordPress don't let you to do this because of missing security nonce or referrer page.我认为 WordPress 不允许您这样做,因为缺少安全随机数或引用页面。

My suggestion is to install/develop a plugin that supports remote login and then send the login request to your website based on the plugin structure.我的建议是安装/开发一个支持远程登录的插件,然后根据插件结构将登录请求发送到您的网站。

You can use following function for auto login您可以使用以下功能进行自动登录

function auto_login( $username ) {
    // log in automatically
    if ( !is_user_logged_in() ) {
        $user = get_userdatabylogin( $username );
        $user_id = $user->ID;
        wp_set_current_user( $user_id, $user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user_login );
    }     
}

Then what you need is to just call it as following:那么你需要的只是调用它如下:

auto_login('admin');

Hope it helps.希望能帮助到你。

I wasnt able to use cURL to log in, but i was able to do things a much much simpler way using the below code.我无法使用 cURL 登录,但我能够使用以下代码以更简单的方式完成任务。

<html>

<head>

    <title>Crunchify Login Page</title>
    <script>
        function loginForm() {
            document.myform.submit();
            document.myform.action = "http://www.example.com/wp-login.php";
        }
    </script>
</head>

<body onload="loginForm()">
<form action="http://www.example.com/wp-login.php" name="myform" method="post">
    <input type="text" name="log" value="admin">
    <input type="password" name="pwd" value="password">
    <input type="submit" value="Login">
</form>

</body>

</html>

This automatically submitted a login request to Wordpress with no problems and logs the user in "Granted the correct details are submitted", simple and effective and also easily scalable using frameworks like VueJS.这会自动向 Wordpress 提交登录请求,没有任何问题,并将用户记录为“已提交正确的详细信息”,简单有效,并且使用 VueJS 等框架也可以轻松扩展。

Looks like wordpress returns back a cookie that it expects to be posted in the headers or else you get this error.看起来 wordpress 会返回一个它希望发布在标题中的 cookie,否则您会收到此错误。 I was able to avoid this by tricking it into thinking i got the cookie ahead of time:我能够通过欺骗它认为我提前拿到了饼干来避免这种情况:

$headers = array(
   "Cookie: wordpress_test_cookie=WP+Cookie+check"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

You also need to make sure you have the cookie options on你还需要确保你有 cookie 选项

curl_setopt($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, '/path/to/cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, '/path/to/cookie.txt');

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

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