December 26th, 2012

tip  |   development  |   skype

Update: as of 12/27/2012, 6pm, Skype mystatus API works again.

Some days ago Skype's mystatus.skype.com has not given the proper API response. Calling http://mystatus.skype.com/sven.kauber.num (or sven.kauber.txt or finally sven.kauber.xml) results in server error or a 1x1 pixel image.

In order to still get the status, the following method can be used:
1. Use url_fopen or cURL to load the status image from the URL http://mystatus.skype.com/balloon/sven.kauber
2. Save the image to a temporary location and get the image from there
3. Each image has different background color, so get just one pixel from the image and RGB values from it
4. Determine the status by using the RGB value combination

Below is the function that is based on the one that was in the earlier Skype-related blog post.

Please note: if you use this function for your purposes, remember to change the value of $tmpFile to match the directory where you are planning to let the function save the temporary status images.
 

  1. public function actionSkypeStatus() {
  2.    if(isset($_GET['username'])) {
  3.       $userName = strip_tags($_GET['username']);
  4.       $status = '1'; // default: offline     
  5.       $remoteImg = "http://mystatus.skype.com/balloon/$userName";
  6.       $tmpFile = dirname(__FILE__). '/../../tmp/status.png';
  7.  
  8.       // removing the existing file to write a new one
  9.       if( file_exists( $tmpFile ) && is_file($tmpFile) ){
  10.          unlink($tmpFile);
  11.       }
  12.  
  13.       // saving the new temporary image
  14.       if( ini_get( 'allow_url_fopen' ) !== '1' ) {
  15.          ini_set('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
  16.          $tmpImg = imagecreatefrompng($remoteImg);
  17.          imagepng($tmpImg, $tmpFile);
  18.       }
  19.       else if( in_array( 'curl', get_loaded_extensions() ) ){
  20.          $ch = curl_init( $remoteImg );
  21.          curl_setopt( $ch, CURLOPT_HEADER, 0 );
  22.          curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  23.          curl_setopt( $ch, CURLOPT_BINARYTRANSFER, 1 );
  24.          curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1' );
  25.          $rawData = curl_exec( $ch );
  26.          curl_close( $ch );
  27.          
  28.          $fp = fopen($tmpFile,'x');
  29.          fwrite($fp, $rawData);
  30.          fclose($fp);
  31.       }
  32.       else {
  33.          throw new Exception('Please either enable fopen or cURL!');
  34.       }
  35.  
  36.       if(file_exists($tmpFile) && is_file($tmpFile)) {
  37.          if( function_exists( 'gd_info' ) ) {
  38.             // getting one pixel from each image and setting status by its color
  39.             $img = imagecreatefrompng($tmpFile);
  40.             $rgb = imagecolorat($img, 70, 40);
  41.             $color = join( ',', array_values( imagecolorsforindex( $img, $rgb ) ) );
  42.            
  43.             switch($color) {
  44.                case '175,180,180,0':
  45.                   $status = '1'; // offline or invisible
  46.                break;
  47.                case '133,197,10,0':
  48.                   $status = '2'; // online
  49.                break;
  50.                case '255,204,0,0':
  51.                   $status = '3'; // away
  52.                break;
  53.                case '39,59,83,0':
  54.                   $status = '5'; // do not disturb
  55.                break;
  56.             }
  57.          } else {
  58.             throw new Exception('No GD library available!');
  59.          }
  60.       }
  61.            
  62.       echo $status;
  63.    }
  64. }