在 PHP 中,使用 cURL 或 fsockopen 实现 WordPress 自动登录的方法如下:
<?php
$username = 'your_username';
$password = 'your_password';
$login_url = 'http://your-wordpress-site.com/wp-login.php';
$login_data = array(
'log' => $username,
'pwd' => $password,
'wp-submit' => 'Log In',
'redirect_to' => 'http://your-wordpress-site.com/wp-admin/',
'testcookie' => '1'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($login_data));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
<?php
$username = 'your_username';
$password = 'your_password';
$login_url = 'http://your-wordpress-site.com/wp-login.php';
$login_data = array(
'log' => $username,
'pwd' => $password,
'wp-submit' => 'Log In',
'redirect_to' => 'http://your-wordpress-site.com/wp-admin/',
'testcookie' => '1'
);
$post_data = http_build_query($login_data);
$length = strlen($post_data);
$fp = fsockopen('your-wordpress-site.com', 80, $errno, $errstr, 30);
if (!$fp) {
echo "Error: $errstr ($errno)";
} else {
$out = "POST /wp-login.php HTTP/1.1\r\n";
$out .= "Host: your-wordpress-site.com\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-Length: " . $length . "\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= $post_data;
fwrite($fp, $out);
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
echo $result;
}
?>
这两种方法都可以实现在 PHP 中使用 cURL 或 fsockopen 进行 WordPress 自动登录。其中,cURL 是更为推荐的方法,因为它更加灵活,功能更强大。
领取专属 10元无门槛券
手把手带您无忧上云