[英]Can you use curl to send a login post request to wordpress?
我目前正在尝试设置一些东西,让我可以远程登录 wordpress,我不确定我是否正确或是否可能,但它似乎来自我读过的一些东西。
我使用下面的代码:
// 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);
当我死 var_dump 时,页面返回这个,没有别的: string(0) ""
就是这样。
但是当我从 curl 请求中删除“pwd”字段时,它会返回登录页面以及一条错误消息,指出密码字段中的密码丢失
编辑
成功登录时出现新错误:
错误:您的浏览器阻止或不支持 Cookie。 您必须启用 cookie 才能使用 WordPress。
我不知道这是什么或为什么这样做
我认为 WordPress 不允许您这样做,因为缺少安全随机数或引用页面。
我的建议是安装/开发一个支持远程登录的插件,然后根据插件结构将登录请求发送到您的网站。
您可以使用以下功能进行自动登录
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 );
}
}
那么你需要的只是调用它如下:
auto_login('admin');
希望能帮助到你。
我无法使用 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>
这会自动向 Wordpress 提交登录请求,没有任何问题,并将用户记录为“已提交正确的详细信息”,简单有效,并且使用 VueJS 等框架也可以轻松扩展。
看起来 wordpress 会返回一个它希望发布在标题中的 cookie,否则您会收到此错误。 我能够通过欺骗它认为我提前拿到了饼干来避免这种情况:
$headers = array(
"Cookie: wordpress_test_cookie=WP+Cookie+check"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
你还需要确保你有 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.