It may be necessary to add to the site a display of the current network status of contacts of various messaging services. Most often this can be found on business cards or to display the availability of the online support operator. You can use for this official services or unofficial, which, as a rule, provide more opportunities for registration, different themes with pictures. But what to do if we have the original site and default images do not fit into its style? You have to either reinvent your bike, which is unlikely to be better than the existing ones, or borrow ideas from ready-made solutions and adapt them to our needs. We will go the second way.
First, let's define our tasks:
- if possible, use official status services
- in case of their absence, you can resort to outsiders
- the method should be as simple as possible, without surplus (you can always expand it later)
- so that the page will not be drawn at the time of the request, we will make a buffer
')
Consider 4 basic ways on the example of ICQ, Jabber, Skype and MRA. The rest can be done similarly.
Verification will be carried out using php. Create a file “status.php” and add its execution to the scheduler every 5 minutes. The status will be saved in a separate file.
In my case, for each protocol its own file, for example "icq.s". Because I have saved not only the status text, but also a picture, then a part of the html-code is written in two lines. So that later on the page of the site not to tear out paragraphs, I decided to simply insert the desired piece in a certain place on the page:
<? include('icq.s'); ?>
It will be possible to record arbitrarily large and complex structures.
ICQ status
We will use the official status check service. It also has its drawbacks, it works strange. For example, the status of "Away" he gives only when the contact is the status of "Invisible". In other cases, except offline, the contact is always displayed "Online". Therefore, if you put the status “Away” in the client, it will still appear as “Online”. But this is not a reason to refuse, we will use the official service, as was stated in the tasks.
Do not forget to allow the client to display status on sites. There are many ways to determine status, but they use the same address for verification:
http://status.icq.com/online.gif?icq=__UIN
Depending on the current ICQ status, the address of the image being sent will take on the values:
http://status.icq.com/0/online0.gif - Away http://status.icq.com/0/online1.gif - Online http://status.icq.com/0/online2.gif - Offline
Depending on the number in the name of the picture, the status will be determined. We will receive the “Location” header from the server response and capture that number from there. And then with the help of switch we will set what will be given as status. The design is as follows:
try { $a = @get_headers('http://status.icq.com/online.gif?icq=__UIN'); foreach($a as $Header) { if (is_int(strpos($Header, 'Location'))) { $Status = substr($Header, -5, 1); } } switch ($Status){ case '0': $icqstatus = '<img src="icq-away.png" alt=""/>'; break; case '1': $icqstatus = '<img src="icq-online.png" alt=""/>'; break; case '2': $icqstatus = '<img src="icq-offline.png" alt=""/>'; break; default: $icqstatus = '<img src="icq-offline.png" alt=""/>'; break; } } catch (Exception $e){}
The variable "icqstatus" stores the current status. If the service does not work, then the status is given "Offline". Now write this status in the file buffer:
$f=fopen('/home/mysite/icq.s','w'); fwrite($f,$icqstatus); fclose($f);
Please note that you need to specify the full path to the file on the server.
As I already wrote above, to display the status in the right place of the page we insert:
<? include('icq.s'); ?>
UPD: changes on ICQ siteNow, instead of three options, there are only “online” or “offline”. Change the code as follows:
View work optionDepending on the current ICQ status, the address of the image being sent will take on the values:
http://status.icq.com/0/online1.gif - Online
All other values ​​will mean Offline. So we write:
try { $a = @get_headers('http://status.icq.com/online.gif?icq=__UIN'); foreach($a as $Header) { if (is_int(strpos($Header, 'Location'))) { $Status = substr($Header, -5, 1); } } switch ($Status){ case '1': $icqstatus = '<img src="icq-online.png" alt=""/>'; break; default: $icqstatus = '<img src="icq-offline.png" alt=""/>'; break; } } catch (Exception $e){}
The rest is as usual.
Skype status
There is an official Skype status checker here. Unlike ICQ, we have a great opportunity to get an answer in the form of a status text. Use this link for this:
http://mystatus.skype.com/_.txt
Do not forget to allow the client to display status on sites. Depending on the current status of Skype, we get the following values:
Online Away Do Not Disturb Offline
We just have to do how to get the contents of a text file without any additional gestures, it is already kosher and does not contain anything extra.
try { $a = @file_get_contents("http://mystatus.skype.com/_.txt"); switch($a) { case 'Online': $skypestatus = '<img src="skype-online.png" alt=""/>'; break; case 'Away': case 'Do Not Disturb': $skypestatus = '<img src="skype-away.png" alt=""/>'; break; case 'Offline': $skypestatus = '<img src="skype-offline.png" alt=""/>'; break; default: $skypestatus = '<img src="skype-offline.png" alt=""/>'; break; } } catch (Exception $e){}
And traditionally, we write the received status from the variable "skypestatus" to the file:
$f=fopen('/home/mysite/skype.s','w'); fwrite($f,$skypestatus); fclose($f);
In the right place of the site will display:
<? include('skype.s'); ?>
Jabber status
This is exactly the case when we cannot do without the use of third-party services. There are no established ways to determine the network status of Jabber. But there are good people. Truth and way more complicated.
At first I used this service
http://web-apps.ru/jabber-presence/ and for a long time it worked perfectly. But I recently went offline for a week and had to look for alternatives, one of which was
http://presence.jabberfr.org/ . We will use it.
Go to the page of the French service, write your JID and at the bottom select "Your status (text)". Just below the generated address will appear, by which the status will be determined.
http://presence.jabberfr.org/____JID/text-en.txt
Now we need to add a presence.jabberr.org service to the roster. He will appear in the contact list and will have the same status as you. To enable the display of the status of the link generated above, we will send the bot a message with the text “visible” without quotes.
Depending on the current status of Jabber, we have the following values:
Available Away Not available Do not disturb Free for chat Offline
If the status is “Invisible”, the last used status will be shown before hiding.
We implement the display of the status obtained on our website. Also, as in the case of Skype, we get just a text file. In addition to the status it contains nothing more.
try { $a = @file_get_contents('http://presence.jabberfr.org/____JID/text-en.txt'); switch ($a){ case 'Available': case 'Free for chat': $jabberstatus = '<img src="jabber-online.png" alt=""/>'; break; case 'Away': case 'Not available': case 'Do not disturb': $jabberstatus = '<img src="jabber-away.png" alt=""/>'; break; case 'Offline': $jabberstatus = '<img src="jabber-offline.png" alt=""/>'; break; default: $jabberstatus = '<img src="jabber-offline.png" alt=""/>'; break; } } catch (Exception $e){}
The jabberstatus variable contains the current status. Write it in the file “jabber.s”:
$f=fopen('/home/mysite/jabber.s','w'); fwrite($f,$jabberstatus); fclose($f);
In the right place of the site we will display the status:
<? include('jabber.s'); ?>
UPD: alternativeBecause Now the French service has fallen, I decided that it is better to write an alternative, i.e.
http://web-apps.ru/jabber-presence/ . Our domestic friend. One of them will work.
View work optionGo to the page
http://web-apps.ru/jabber-presence/ and read the description. For the lazy, I'll write everything in steps.
Add to our roster web-apps-presence@jabber.ru. After adding the message will come with a brief help. We will need to remember the contents of the html string: the first one at jabber, the second one by its md5 hash. In the example, the second option will be used.
http://web-apps.ru/jabber-presence/html/xid/____JID
Now we write to the bot bot a message containing only the number 1. We will get an answer that the status display is enabled.
Further we will set the type of the displayed status, we need only the text. To do this, send in a personal bot a message:
set html=%{status}
Depending on the current status of Jabber, we have the following values:
available away xa dnd chat
If the status is “Invisible”, the last used status will be shown before hiding. All other cases equate to offline.
We implement the display of the status obtained on our website.
try { $a = @file_get_contents('http://web-apps.ru/jabber-presence/html/xid/____JID'); switch ($a){ case 'available': case 'chat': $jabberstatus = '<img src="jabber-online.png" alt=""/>'; break; case 'away': case 'xa': case 'dnd': $jabberstatus = '<img src="jabber-away.png" alt=""/>'; break; default: $jabberstatus = '<img src="jabber-offline.png" alt=""/>'; break; } } catch (Exception $e){}
The rest is as usual.
MRA status
This is Mail.ru Agent. Again we will use the official service of checking the status of Mail.ru Agent. Like ICQ, we are given the opportunity to display only a picture, without status text:
http://status.mail.ru/?_
Because all images have the same address and all of the same size, you have to come up with something else. You can find out their weight, but this is not the best way. Let's make a knight's move, I mean, we will simply determine the md5-hash of the image. We also get three types of statuses:
0318014f28082ac7f2806171029266ef - Online, Free for chat, Do not disturb 89d1bfcdbf238e7faa6aeb278c27b676 - Away a46f044e175e9b1b28c8d9a9f66f4495 - Offline, Invisible
After determining the hash with a familiar construction, we set the status:
try { $a = @md5(file_get_contents("http://status.mail.ru/?_")); switch($a) { case '0318014f28082ac7f2806171029266ef': $mrastatus = '<img src="mra-online.png" alt=""/>'; break; case '89d1bfcdbf238e7faa6aeb278c27b676': $mrastatus = '<img src="mra-away.png" alt=""/>'; break; case 'a46f044e175e9b1b28c8d9a9f66f4495': $mrastatus = '<img src="mra-offline.png" alt=""/>'; break; default: $mrastatus = '<img src="mra-offline.png" alt=""/>'; break; } } catch (Exception $e){}
Write the resulting MRA status to the file:
$f=fopen('/home/mysite/mra.s','w'); fwrite($f,$mrastatus); fclose($f);
And in the right place we will display the status:
<? include('mra.s'); ?>
Check how it works, you can here
http://damaks.me/ . There is a lot of work, I will try to stay in the network a little longer so that you can see this action with your own eyes.
Other protocols
Similarly, we can check the current network status of other protocols. If there is an official service, then use it, if not, then use alternative ones. Here are 4 different ways, at least one of which is suitable for other protocols.
Vkontakte statusWe receive the status (it is possible to use both numeric id, and an alias):
try { $ch = curl_init('https://api.vkontakte.ru/method/getProfiles?uids=__ID&fields=online'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec($ch); curl_close($ch); $a = substr($result, -4, 1); switch ($a){ case '1': $vkstatus = '<img src="vk-online.png" alt=""/>'; break; case '0': $vkstatus = '<img src="vk-offline.png" alt=""/>'; break; default: $vkstatus = '<img src="vk-offline.png" alt=""/>'; break; } } catch (Exception $e){}
Write it to the file:
$f=fopen('/home/mysite/vk.s','w'); fwrite($f,$vkstatus); fclose($f);
And where you need to display:
<? include('vk.s'); ?>
PS
I found different ways of displaying statuses on different resources, I can’t definitely remember. Especially there are the same descriptions many times. If the source is known, I will add to the post. If there are comments / suggestions, I will listen with pleasure.