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);
}
An endless runing CLI application in PHP in not the best solution. Its better when you make a cronjob and run the script every one or every to minutes. Then you can store the status or what you need in a database or a file.
I have looked at your scripted and test the script:
<?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);
}
Its not the best solution but the problem in your case is, that you can't write in the $user
output. I have made here a new variable with the user id as iterator. When you run the script you can see the output:
User 0: online
User 1: offline
User 0: online
User 1: error
User 0: online
User 1: offline