PHP sends and receives text messages through GSMModem-PHP source code

Source: Internet
Author: User
PHP through the GSM modem to send and receive text messages the code using php-serial Project serial communication, complete code package download: https://github.com/gonzalo123/gam-sms
Related AT instruction sets:
AT + CPIN? \ R: checks if SIM has the pin code. It answers + CPIN: READY or + CPIN: sim pin if we need to insert the pin number
AT + CMGS = "[number]" \ r [text] Control-Z: to send a SMS (in php we have Control-Z with chr (26 )). it returns OK or ERROR
AT + CMGF = 1 \ r: set the device to operate in SMS text mode (0 = PDU mode and 1 = text mode). Returns OK.
AT + CMGL = \ "ALL" \ r read all the sms stored in the device. We also can use "rec unread" Instead of "ALL ".
AT + CMGD = [ID] \ r: Deletes a SMS from the device

1. PHP code

 _validOutputs = $validOutputs;    }}

2. test class

 insertPin($pin)                ->sendSMS(555987654, "test Hi")) {    echo "SMS sent\n";} else {    echo "SMS not Sent\n";}

3. complete class

require_once('Sms.php');require_once('Sms/Interface.php');require_once('Sms/Serial.php'); $pin = 1234; try {    $serial = new Sms_Serial;    $serial->deviceSet("/dev/ttyS0");    $serial->confBaudRate(9600);    $serial->confParity('none');    $serial->confCharacterLength(8);     $sms = Sms::factory($serial)->insertPin($pin);     if ($sms->sendSMS(555987654, "test Hi")) {        echo "SMS sent\n";    } else {        echo "Sent Error\n";    }     // Now read inbox    foreach ($sms->readInbox() as $in) {        echo"tlfn: {$in['tlfn']} date: {$in['date']} {$in['hour']}\n{$in['msg']}\n";         // now delete sms        if ($sms->deleteSms($in['id'])) {            echo "SMS Deleted\n";        }    }} catch (Exception $e) {    switch ($e->getCode()) {        case Sms::EXCEPTION_NO_PIN:            echo "PIN Not set\n";            break;        case Sms::EXCEPTION_PIN_ERROR:            echo "PIN Incorrect\n";            break;        case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:            echo "Service Not implemented\n";            break;        default:            echo $e->getMessage();    }}

4. PHP code

 insertPin($pin);     if ($sms->sendSMS(555987654, "test Hi")) {        echo "SMS Sent\n";    } else {        echo "Sent Error\n";    }     // Now read inbox    foreach ($sms->readInbox() as $in) {        echo"tlfn: {$in['tlfn']} date: {$in['date']} {$in['hour']}\n{$in['msg']}\n";         // now delete sms        if ($sms->deleteSms($in['id'])) {            echo "SMS Deleted\n";        }    }} catch (Exception $e) {    switch ($e->getCode()) {        case Sms::EXCEPTION_NO_PIN:            echo "PIN Not set\n";            break;        case Sms::EXCEPTION_PIN_ERROR:            echo "PIN Incorrect\n";            break;        case Sms::EXCEPTION_SERVICE_NOT_IMPLEMENTED:            echo "Service Not implemented\n";            break;        default:            echo $e->getMessage();    }}

The above is the content of text messages sent and received by PHP via GSM Modem. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.