调用 MediaWiki-API 时,我只得到 '+\' 作为 csrf-token

When calling the MediaWiki-API I only get '+\' as an csrf-token

我可以使用简单标记“+\”进行编辑,但是使用这个简单标记,我只能以未注册 IP 的身份进行编辑,而不能以注册用户的身份进行编辑。有人可以帮助我吗?

我的PHP代码:

$parameters = array('action' => 'query', 'meta' => 'tokens', 'format' => 'json');
$options = array(
   'http' => array(
      'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
      'method'  => 'POST',
      'content' => http_build_query($parameters),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($wiki, false, $context);
echo "$result";

您需要登录才能获得真正的 CSRF 令牌。登录方式详见:

http://www.mediawiki.org/wiki/API:Login

请注意,保持登录状态需要您跨请求存储 cookie。 file_get_contents() 很难做到这一点;建议将 cURL 与 cookie jar 一起使用。

要获得 login token 使用 type=login 参数。示例:

curl 'https://www.mediawiki.org/w/api.php?action=query&meta=tokens&format=json&type=login'