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 )!