php - 在 PHP 中使用 CURL 或 fsockopen 的 Wordpress 自动登录

标签 php curl fsockopen

客户想要点击一个链接并自动登录到 Wordpress 后端管理部分。

我尝试使用 fsockopen,代码如下。没用。

$post_data['user_login'] = 'admin';
$post_data['user_pass'] = 'password';
$post_data['wp-submit'] = 'Log In';
$post_data['redirect_to'] = 'http://example.com/wp-admin/';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;

$data_length = strlen($post_string);

$connection = fsockopen('www.example.com', 80);

fputs($connection, "POST /wp-login.php HTTP/1.1\r\n");
fputs($connection, "Host: www.example.com \r\n");
fputs($connection, "Content-Type: application/x-www-form-urlencoded\r\n");
fputs($connection, "Content-Length: $data_length\r\n");
fputs($connection, "Connection: close\r\n\r\n");
fputs($connection, $post_string);


fclose($connection);

也试过 CURL

$ch = curl_init('http://example.com/wp-login.php');

$post_data['user_login'] = 'admin';
$post_data['user_pass'] = 'password';
$post_data['wp-submit'] = 'Log In';
$post_data['redirect_to'] = 'http://example.com/wp-admin/';
//$post_data['testcookie'] = '0';
//$post_data['rememberme'] = 'forever';

foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);




curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_exec ($ch);
curl_close ($ch); 

有人知道如何使这项工作成功吗?

它是一个 linux 操作系统。运行 php5。

我以前用 javascript 完成过这个,只是在页面加载时提交一个包含所有隐藏输入的表单。客户端不需要javascript

最佳答案

这对我有用:

$username="admin";
$password="admin";
$url="http://www.yourdomain.com/";
$cookie="cookie.txt";

$postdata = "log=". $username ."&pwd=". $password ."&wp-submit=Log%20In&redirect_to=". $url ."wp-admin/&testcookie=1";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "wp-login.php");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "wp-admin/");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
exit;

关于php - 在 PHP 中使用 CURL 或 fsockopen 的 Wordpress 自动登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/724107/

相关文章:

PHP 和 cURL : same code but unable to fetch content on existing server conf

php - 如何在 cURL 上使用 TLS 协议(protocol)?

node.js - npm 无法从 registry.npmjs.org 安装

php - Google App Engine中的PHP cURL HTTP代码0

php - 如何在 PHP 中将 Socks5 与 fsockopen 结合使用

php - Symfony httplug 包句柄缺少 EventSubscriberInterface

mysql - PDO 使用 bindParam 返回 MySQL 错误

php - 使用山魈发送邮件。异步不起作用

php - 使用fsockopen进行PHP GET和POST数据

php - 如何异步运行多个 fsockopen()