1

I've been trying to make a CLI application that logs Yahoo! messenger login dates/times for certain users using a third party, but this isn't really getting anywhere. Even though checking iself works when used individually, it does not seem to when using the while & foreach too. checkAvailability outputs "000". Could anyone please fix this and perhaps optimize it?

<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";

function checkAvailability($user){
    $dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
    $fullText = explode("|", $dataGot);
    $status_coded = $fullText[0];
    echo $status_coded;
    return $status_coded;
}


while(true) {
    foreach($users as $user) {
        $user['oldstatus'] = $user['status'];

        if (checkAvailability($user) == "1" and $user['oldstatus'] != "online") {
            $user['status'] = "online";
            echo "online";
        } elseif (checkAvailability($user) == "3" and $user['oldstatus'] != "invisible") {
            $user['status'] = "invisible";
            echo "invisible";
        } elseif (checkAvailability($user) == "2" and $user['oldstatus'] != "offline") {
            $user['status'] = "offline";
            echo "offline";
        } else {
            $user['status'] = "error";
            echo "error";
        }
        if ($user['status'] != $user['oldstatus']) {
            echo $user." a fost detectat ca ".$user['status']." la ".date(DATE_RFC822).".\n";
        }
    }
    sleep(60);
    sleep(60);
}
4

1 に答える 1

0

PHPで無限に実行されるCLIアプリケーションは、最善のソリューションではありません。cronジョブを作成し、スクリプトを1分ごとまたは1分ごとに実行するとより効果的です。次に、ステータスまたは必要なものをデータベースまたはファイルに保存できます。

私はあなたのスクリプトを見て、スクリプトをテストしました:

<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";

function checkAvailability($user){
    $dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
    $fullText = explode("|", $dataGot);
    $status_coded = $fullText[0];
    return $status_coded;
}

while(true) {
    foreach($users as $key => $user) {
        $userStatus[$key] = checkAvailability($user);     

        if(!isset($userStatusRet[$key]['oldstatus'])) {
            $userStatusRet[$key]['oldstatus'] = '';
        }

        if(!isset($userStatusRet[$key]['status'])) {
            $userStatusRet[$key]['status'] = '';
        }

        $userStatusRet[$key]['oldstatus'] = $userStatusRet[$key]['status'];

        if ($userStatus[$key] == "1" and $userStatusRet[$key]['oldstatus'] != "online") {
            $userStatusRet['status'] = "online";
            echo "User ".$key.": online\n";
        } elseif ($userStatus[$key] == "3" and $userStatusRet[$key]['oldstatus'] != "invisible") {
            $userStats['status'] = "invisible";
           echo "User ".$key.": invisible\n";
        } elseif ($userStatus[$key] == "2" and $userStatusRet[$key]['oldstatus'] != "offline") {
            $userStatusRet[$key]['status'] = "offline";
            echo "User ".$key.": offline\n";
        } else {
            $userStatusRet[$key]['status'] = "error";
            echo "User ".$key.": error\n";
        }
    }
    sleep(5);
}

$userこれは最善の解決策ではありませんが、あなたの場合の問題は、出力に書き込めないことです。ここでは、ユーザーIDをイテレーターとする新しい変数を作成しました。スクリプトを実行すると、次の出力が表示されます。

User 0: online
User 1: offline
User 0: online
User 1: error
User 0: online
User 1: offline
于 2012-12-15T01:43:53.217 に答える