如何使用API​​登录或clientlogin自动登录mediawiki?

时间:2017-05-11 13:48:12

标签: php curl cookies mediawiki mediawiki-api

大多数指南我都可以在版本1.27之前找到工作。感到如此迷惑如何设置cookie?我如何获得sessionid?
这是我的代码,但没有正确获得登录令牌 系统信息:
软件版本
MediaWiki 1.28.2
PHP 5.6.30
MariaDB 10.1.21

<?php
namespace mediawiki;


// Start session
session_start();

/**
 * How to log in mediawiki using PHP cURL?
 * -------------------------------------------------
 */

//set login username password which already in your mediawiki database
$username = 'abc';
$password = '123';

//setup url
$Root = 'localhost/mediawiki';
$API_Location = "${Root}/api.php";

//setup cookie
$CookieFilePath = tempnam("/tmp", "TMP0");
$expire = 60*60*24*14 + time();
$CookiePrefix = 'theprefix';
$Domain = 'localhost';

// set variables to use in curl_setopts
$PostFields = "action=query&meta=tokens&type=login&format=json";

// first http post to sign in to MediaWiki
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded',
    'Content-Length: ' .strlen($PostFields))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$PostFields");

curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);

$Result = curl_exec($ch);
if(curl_exec($ch) === false) echo '<br>Curl error: ' . curl_error($ch).'<br>';
curl_close($ch); // curl closed

$ResultSerialized = json_decode($Result,true);
$Token = $ResultSerialized["query"]["tokens"]["logintoken"];

// cookie must be set using session id from first response
$_SESSION["logintoken"]=$Token;
//How can I get sessionid?
$sessionid=session_id();
$_SESSION["sessionid"] =$sessionid;

setcookie("${CookiePrefix}_Session",$sessionid , $expire, '/', $Domain);
setcookie("${CookiePrefix}UserName",$username,$expire,'/',$Domain);
setcookie("${CookiePrefix}Token", $_SESSION["logintoken"], $expire, '/', $Domain);

// second http post to finish sign in
$ch = curl_init();
$PostFields="action=login&lgname=${username}&lgpassword=${password}&lgtoken=${Token}&format=json";
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
       'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' .strlen($PostFields))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$PostFields");
curl_setopt($ch, CURLOPT_COOKIE, "${CookiePrefix}_session=$sessionid");

curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);

$Result = curl_exec($ch);
if(curl_exec($ch) === false) echo '<br>Curl error: ' . curl_error($ch).'<br>';
curl_close($ch); // curl closed
$ResultSerialized = json_decode($Result,true);

// set persistent cookies
//$LgToken = $ResultSerialized["query"]["tokens"]["logintoken"];
$LgUserID = $ResultSerialized["login"]["lguserid"];
$LgUserName = $ResultSerialized["login"]["lgusername"];
$lgstatus=$ResultSerialized["login"]["result"];
var_dump($lgstatus);

setcookie("${CookiePrefix}UserName", $LgUserName, $expire, '/', $Domain);
setcookie("${CookiePrefix}UserID", $LgUserID, $expire, '/', $Domain);
//setcookie("${CookiePrefix}Token", $Token, $expire, '/', $Domain);

// Delete cURL cookie
unlink($CookieFilePath);

?>

我还尝试通过邮递员使用clientlogin,发布请求与mediawiki.org/wiki/API:Login上的示例完全相同,但结果为:“authmanager-authn-no-primary”。

参考:

1 个答案:

答案 0 :(得分:0)

要登录,您需要首先获得logintoken

$query_string = '?action=query&meta=tokens&type=login&format=json';
...// sparing curl_exec details
$ResultSerialized = json_decode($Result, true);
$Token = $ResultSerialized['query']['tokens']['logintoken'];

然后,您发布logintoken以及您的用户名和密码:

$post_data = "?action=login&lgname=$username&lgpassword=password&logintoken=$Token&format=json";

你应该登录!只要您在所有请求之间保持相同的cookie会话,您就应该能够使用登录的操作。