0

フォーラムランナーAPIを使用して、フォーラムのユーザーにメッセージを外部に送信しようとして問題が発生しました。

私はこのコードで正常にログインすることができました:

function login($username, $password){
    $postdata = "cmd=login&username=".$username."&password=".$password;

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    $result = file_get_contents('http://forums.<<<website>>>.org/forumrunner/request.php', false, $context);
    $resultantArray = json_decode($result, true);

    if($resultantArray['success'] == true){
        $this->loginresult=$resultantArray;
        return true;
    } else {
        $this->loginresult=false;
        return false;
    }
}

結果:

array(5) { ["success"]=> bool(true) ["data"]=> array(6) { ["username"]=> string(8) "<<<Username>>>" ["authenticated"]=> bool(true) ["v"]=> string(5) "1.1.1" ["p"]=> string(5) "xen10" ["requires_authentication"]=> bool(false) ["reg"]=> bool(true) } ["ads"]=> int(0) ["pm_notices"]=> int(0) ["sub_notices"]=> int(0) } string(4395) "

これで機能し、ログイン成功メッセージが返されますが、新しい会話を開始するためのこのコードは機能していません(認証に失敗します)。

function sendPM($recipients, $title, $message) {
    $postdata = "cmd=start_conversation&recipients=".$recipients."&title=".$title."&message=".$message."&d=1";

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    $result = file_get_contents('http://forums.<<<website>>>.org/forumrunner/request.php', false, $context);
    $resultantArray = json_decode($result, true);
    if($resultantArray['success'] == true){
        $this->pmresult=$resultantArray;
        return true;
    } else {
        $this->pmresult=$result;
        return false;
    }

}

そしてエラー(重要な部分):

{"success":false,"message":"You do not have permission to view this page or perform this action."}

ある種のセキュリティトークンを一緒に渡す必要があると思いますが、どこで入手できるかわかりません。

4

1 に答える 1

0

PHP を Web クライアント (Firefox や Chrome など) として使用していますが、それはブラウザーではありません。デフォルトでは Cookie をサポートしていません。

あなたが現在ブラウザを使用しているとしましょう。通常、Web サイトにログインすると、サイトから何らかのトークン (セッション ID、ユーザー名など) が渡されます。トークンはブラウザの Cookie に保存されます。サイトで何かを行うたびに、ブラウザーはそのトークンをサイトに提供します。これにより、サイトは毎回(ログイン後に)ログイン情報を知ることができます。

さて、あなたのプログラムは同じことをしなければなりませんが、PHP は自動的にそれを行いません。次のいずれかを行う必要があります。

  1. 独自のコードを記述して、リクエスト/応答ヘッダーで Cookie を読み取り、保存、および書き込みます。

  2. Web を検索し、関連するライブラリがあれば見つけます。

次のような関連する議論を読むことをお勧めします。

https://forums.digitalpoint.com/threads/how-to-make-a-php-robot-that-c​​an-log-in.2410778/

于 2013-02-25T05:02:43.450 に答える