LoginSignup
Asahi-jp
@Asahi-jp

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

[PHP]MediaWiki Botのエラー

解決したいこと

PHPでMediaWikiのBotを作成していたとき、スクリプト内で指定している、Login failed.のエラーが発生します。MediaWikiのBotについて詳しい方、回答宜しくお願いします。

発生している問題・エラー

[Sat Mar 30 13:13:41.674829 2024] [fcgid:warn] [pid 3388139:tid 3388729] mod_fcgid: stderr: PHP Warning:  Undefined array key "login" in bot.php on line 78
[Sat Mar 30 13:13:41.674854 2024] [fcgid:warn] [pid 3388139:tid 3388729] mod_fcgid: stderr: PHP Warning:  Trying to access array offset on value of type null in bot.php on line 78

該当するソースコード

<?php

// ユーザー名とパスワード
$username = "YourUsername";
$password = "YourPassword";

// APIエンドポイント
$endPoint = "https://www.mediawiki.org/w/api.php";

// ログイントークンの取得
$params = [
    "action" => "query",
    "meta" => "tokens",
    "type" => "login",
    "format" => "json"
];
$url = $endPoint . "?" . http_build_query($params);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output, true);
$loginToken = $result['query']['tokens']['logintoken'];

// ログイン
$params = [
    "action" => "login",
    "lgname" => $username,
    "lgpassword" => $password,
    "lgtoken" => $loginToken,
    "format" => "json"
];
$url = $endPoint;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output, true);

if ($result["login"]["result"] == "Success") {
    echo "Login successful\n";

    // 編集トークンの取得
    $params = [
        "action" => "query",
        "meta" => "tokens",
        "type" => "csrf",
        "format" => "json"
    ];
    $url = $endPoint . "?" . http_build_query($params);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($output, true);
    $editToken = $result['query']['tokens']['csrftoken'];

    // 記事の編集
    $pageTitle = "YourPageTitle";
    $newContent = "YourNewContent";
    $params = [
        "action" => "edit",
        "title" => $pageTitle,
        "token" => $editToken,
        "text" => $newContent,
        "format" => "json"
    ];
    $url = $endPoint;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    $result = json_decode($output, true);

    if (isset($result["edit"]["result"]) && $result["edit"]["result"] == "Success") {
        echo "Page edited successfully\n";
    } else {
        echo "Error editing page\n";
    }
} else {
    echo "Login failed\n";
}

?>

自分で試したこと

・パスワードの確認
・API.phpのリンク確認

0

No Answers yet.

Your answer might help someone💌