Tuesday, 26 March 2013

Sending SMS messages Using Skype with PHP and Skype4COM


Sending SMS messages 
Using Skype with PHP and Skype4COM

Now normally this is not a problem. I am one of those people who usually check their e-mail several times an hour, so I would get the notifications within about 15 minutes. More than fast enough for the particular website I was monitoring. However, another website which also lists accommodations has a much faster reading-rate. What is more, they have accommodations on a first-come-first-serve basis. Time really is of the essence when you want to get an appartment that way. Now it is fairly easy to get a PHP script to beep your PC speaker - just output the right character:
Code (php) (nieuw venster):
1
echo chr(7);

But my PC speaker is not very loud, and I will not hear it when I am in the next room, or listening to music. So in addition, my new script also sends me a text message whenever a last-minute accommodation becomes available. To do this, I used Skype, which offers a public API for just such needs.

Installing Skype4COM
The first step is to download Skype4COM. Extract the DLL file somewhere it can be found - for example, your windows/system32 directory. Check the properties and unblock it if you need to. 

When you have succesfully extracted the Skype4COM dll, open a command line and navigate to the directory where you extracted it. Then, run regsvr32 to register it:
Code (php) (nieuw venster):
1
regsvr32 Skype4COM.dll

It should give you a notice that it succesfully registred Skype4COM. 

Using Skype4COM in PHP
PHP offers a COM class to handle COM objects. The following example classes illustrate how you can use it to send an SMS message through Skype:

Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 *  Skype COM interface class.
 *
 *  Basic class to handle talking to a Skype client.
**/

class Skype {

  const     LIBRARY             = "Skype4COM.Skype",    // Library name.
            SMS_TYPE_OUTGOING   = 1;                    // smsMessageTypeOutgoing.

  private   $com                = null,                 // COM object.
            $sink               = null,                 // Skype event sink object.
            $convert            = null;                 // Skype Convert object.


  /**
   *  Create and initiate a new Skype COM object handler.
  **/

  public function __construct () {
    $this -> com    = new COM(self::LIBRARY);
    $this -> sink   = new SkypeEventSink();
    $this -> sink  -> convert = $this -> com -> convert();

    com_event_sink($this -> com, $this -> sink, "_ISkypeEvents");

    $this -> convert              = $this -> com -> convert;
    $this -> convert -> language  = "en";
    $this -> checkClient();
  }



  /**
   *  Send a text message through Skype.
   *
   *  @param  target  String    Valid telephone number to send to.
   *  @param  body    String    SMS message body to send. Should be no more
   *                            than 160 alfanumeric chars.
   *  @return         Bool      Either true on success or false on failure.
  **/

  public function sendSms ($target, $body) {
    try {
      $sms            = $this -> com -> CreateSms(self::SMS_TYPE_OUTGOING, $target);
      $sms -> Body    = $body;
      $sms -> Send();
    }
 catch (com_exception $e) {
      print_r($e -> getMessage());
      return
 false;
    }
    return
 true;
  }



  /**
   *  Make sure the Skype client is running. If not, attempt to
   *  start it.
  **/

  private function checkClient () {
    if (!
$this -> com -> client() -> isRunning())
      $this -> com -> client() -> start(true, true);
  }
}



/**
 *  Handle Skype events.
**/

class SkypeEventSink {


  public
    $convert;

  public function
 AttachmentStatus ($status) {
   echo
 ">Attachment status $status\n";
  }


  public function
 CallStatus ($call, $status) {
   echo
 ">Call $call->id status $status\n";
  }
}


Using this class is fairly easy:
Code (php) (nieuw venster):
1
2
$skype  = new Skype();
$skype -> sendSms("+2511223344", "This is a test SMS message.");

No comments:

Post a Comment