3

Am new to PHP. Am working in a recent project which needs to notify clients via sms. I know there are sms services we can use but my boss wants to try using a GSM modem. Is it possible to send SMS using a GSM modem ? Any tutorials will be really helpful.

Thanks

Karthik Nk
  • 377
  • 1
  • 8
  • 25
  • Talking to hardware is outside PHP's scope. You can call external commands from PHP, though. Http://php.net/exec – Pekka Jan 11 '14 at 05:46
  • @Pekka웃 That's just not true. There are plenty of classes available for opening a serial port in PHP, which is what is needed for this task. – Brad Jan 11 '14 at 05:48
  • possible duplicate of [Implementing a GSM Modem for SMS](http://stackoverflow.com/questions/9897873/implementing-a-gsm-modem-for-sms) – Brad Jan 11 '14 at 05:49
  • See also: http://stackoverflow.com/a/4231288/362536 – Brad Jan 11 '14 at 05:49
  • @KarthikNk If you are working in any volume at all, or want to avoid paying a ton of money, do the responsible thing and tell your boss that he is wrong. :-D An SMS gateway is *usually* the answer, unless you have a specific application in mind (such as alerting a handful of folks when your servers go down). – Brad Jan 11 '14 at 05:51
  • @Brad Thanks for response. But sending SMS is just to notify the service engineers of organisation when new work alloted. which brings around 50 sms max per day. that is the reason he want to try with GSM modem. I tried looking for some tutorials but mostly confused with COM ports etc... Any help will be appreciated – Karthik Nk Jan 11 '14 at 06:34
  • @KarthikNk I don't know of any code off the shelf for sending SMS... different devices use different commands, but many are standardized. Look at the documentation that came with your GSM modem. You're looking for the "AT Command Reference" document. – Brad Jan 11 '14 at 06:36
  • Sending SMS via PHP is asked often on Stack Overflow, for example http://stackoverflow.com/questions/9257302/sending-sms-at-commands-to-3g-modem-using-php, http://stackoverflow.com/questions/15212930/send-sms-through-php-at-commands?rq=1, http://stackoverflow.com/questions/5938471/at-commands-php and http://stackoverflow.com/questions/20990533/send-and-read-sms-through-a-gsm-modem-using-at-commands-from-websites are four other php - sms questions. – user1725145 Jan 11 '14 at 09:24

1 Answers1

3
you can use following code, it works on my gsm modem. its zyxel

<?php

//SMS via GSM Modem - A PHP class to send SMS messages via a GSM modem attached to the computers serial port.

//Windows only (tested on XP with PHP 5.2.6)
//Tested with the EZ863 (Telit GE863) GSM modementer code here
//Requires that PHP has permission to access "COM" system device, and system "mode" command

error_reporting(E_ALL);

//Example

$gsm_send_sms = new gsm_send_sms();
$gsm_send_sms->debug = false;
$gsm_send_sms->port = 'COM6';
$gsm_send_sms->baud = 19200;
$gsm_send_sms->init();

$status = $gsm_send_sms->send('09360403533', 'testing 123');
if ($status) {
    echo "Message sent\n";
} else {
    echo "Message not sent\n";
}



$gsm_send_sms->close();







//Send SMS via serial SMS modem
class gsm_send_sms {

    public $port = 'COM1';
    public $baud = 115200;

    public $debug = false;

    private $fp;
    private $buffer;

    //Setup COM port
    public function init() {

        $this->debugmsg("Setting up port: \"{$this->port} @ \"{$this->baud}\" baud");

        exec("MODE {$this->port}: BAUD={$this->baud} PARITY=N DATA=8 STOP=1", $output, $retval);

        if ($retval != 0) {
            throw new Exception('Unable to setup COM port, check it is correct');
        }

        $this->debugmsg(implode("\n", $output));

        $this->debugmsg("Opening port");

        //Open COM port
        $this->fp = fopen($this->port . ':', 'r+');

        //Check port opened
        if (!$this->fp) {
            throw new Exception("Unable to open port \"{$this->port}\"");
        }

        $this->debugmsg("Port opened");
        $this->debugmsg("Checking for responce from modem");

        //Check modem connected
        fputs($this->fp, "AT\r");

        //Wait for ok
        $status = $this->wait_reply("OK\r\n", 5);

        if (!$status) {
            throw new Exception('Did not receive responce from modem');
        }

        $this->debugmsg('Modem connected');

        //Set modem to SMS text mode
        $this->debugmsg('Setting text mode');
        fputs($this->fp, "AT+CMGF=1\r");

        $status = $this->wait_reply("OK\r\n", 5);

        if (!$status) {
            throw new Exception('Unable to set text mode');
        }

        $this->debugmsg('Text mode set');

    }

    //Wait for reply from modem
    private function wait_reply($expected_result, $timeout) {

        $this->debugmsg("Waiting {$timeout} seconds for expected result");

        //Clear buffer
        $this->buffer = '';

        //Set timeout
        $timeoutat = time() + $timeout;

        //Loop until timeout reached (or expected result found)
        do {

            $this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");

            $buffer = fread($this->fp, 1024);
            $this->buffer .= $buffer;

            usleep(200000);//0.2 sec

            $this->debugmsg("Received: {$buffer}");

            //Check if received expected responce
            if (preg_match('/'.preg_quote($expected_result, '/').'$/', $this->buffer)) {
                $this->debugmsg('Found match');
                return true;
                //break;
            } else if (preg_match('/\+CMS ERROR\:\ \d{1,3}\r\n$/', $this->buffer)) {
                return false;
            }

        } while ($timeoutat > time());

        $this->debugmsg('Timed out');

        return false;

    }

    //Print debug messages
    private function debugmsg($message) {

        if ($this->debug == true) {
            $message = preg_replace("%[^\040-\176\n\t]%", '', $message);
            echo $message . "\n";
        }

    }

    //Close port
    public function close() {

        $this->debugmsg('Closing port');
        var_dump($this->fp);
        fclose($this->fp);

    }

    //Send message
    public function send($tel, $message) {
        //Filter tel
        $tel = preg_replace("%[^0-9\+]%", '', $tel);

        //Filter message text
        //$message = preg_replace("%[^\040-\176\r\n\t]%", '', $message);

        $this->debugmsg("Sending message \"{$message}\" to \"{$tel}\"");

        //Start sending of message
        fputs($this->fp, "AT+CMGS=\"{$tel}\"\r");

        //Wait for confirmation
        $status = $this->wait_reply("\r\n> ", 5);

        if (!$status) {
            //throw new Exception('Did not receive confirmation from modem');
            $this->debugmsg('Did not receive confirmation from modem');
            return false;
        }

        //Send message text
         fputs($this->fp, $message);


        //Send message finished indicator
        fputs($this->fp, chr(26));

        //Wait for confirmation
        $status = $this->wait_reply("OK\r\n", 180);

        if (!$status) {
            //throw new Exception('Did not receive confirmation of messgage sent');
            $this->debugmsg('Did not receive confirmation of messgage sent');
            return false;
        }

        $this->debugmsg("Message sent");

        return true;

    }

}

?>
Retro CIB
  • 63
  • 1
  • 7