1

我一直在尝试制作一个记录 Yahoo! 的 CLI 应用程序。某些使用第三方的用户的信使登录日期/时间,但这并没有真正到达任何地方。尽管单独使用时检查 iself 有效,但在使用 while 和 foreach 时似乎也不起作用。checkAvailability 输出“000”。任何人都可以解决这个问题并优化它吗?

<?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 应用程序并不是最佳解决方案。当您制作一个 cronjob 并每隔一分钟或每隔几分钟运行一次脚本时会更好。然后您可以将状态或您需要的内容存储在数据库或文件中。

我查看了您的脚本并测试了脚本:

<?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 回答