├── README.md ├── sms.class.php └── sms.test.php /README.md: -------------------------------------------------------------------------------- 1 | sms 2 | === 3 | 4 | php class for sending and receiving sms via a GSM modem. 5 | -------------------------------------------------------------------------------- /sms.class.php: -------------------------------------------------------------------------------- 1 | intStatus = SMS_STATUS_OK; 56 | 57 | // set port speed with stty 58 | //exec("stty -F /dev/{$strPort} 115200"); 59 | 60 | // open port 61 | $this->_refPort = dio_open("/dev/{$strPort}", O_RDWR | O_NOCTTY | O_NONBLOCK); 62 | 63 | if (!$this->_refPort) 64 | { 65 | $this->intStatus = SMS_STATUS_ERROR; 66 | } 67 | else 68 | { 69 | // set some port crap 70 | dio_fcntl($this->_refPort, F_SETFL, O_SYNC); 71 | 72 | // set port speed etc 73 | dio_tcsetattr($this->_refPort, array( 74 | 'baud' => 19200, 75 | 'bits' => 8, 76 | 'stop' => 1, 77 | 'parity' => 0 78 | )); 79 | 80 | // init modem 81 | if (!$this->command('AT+CFUN=1,1')) 82 | { 83 | $this->intStatus = SMS_STATUS_ERROR; 84 | } 85 | elseif (!$this->command('AT+CMGF=1')) 86 | { 87 | $this->intStatus = SMS_STATUS_ERROR; 88 | } 89 | } 90 | } 91 | 92 | public function write($strCommand) 93 | { 94 | return dio_write($this->_refPort, $strCommand."\r"); 95 | } 96 | 97 | public function readLine() 98 | { 99 | $chrReturn = ""; 100 | $strReturn = ""; 101 | while (1) 102 | { 103 | $chrReturn = dio_read($this->_refPort, 1); 104 | if ($chrReturn == "\n" || $chrReturn == "\r") 105 | { 106 | break; 107 | } 108 | $strReturn .= $chrReturn; 109 | } 110 | echo "{$strReturn}\n"; 111 | return $strReturn; 112 | } 113 | 114 | public function command($strCommand) 115 | { 116 | // clear reply cache 117 | $this->strReply = ""; 118 | 119 | // send command 120 | if (!$this->write($strCommand)) 121 | { 122 | return FALSE; 123 | } 124 | 125 | // clear response 126 | $strReply = ""; 127 | 128 | // get response 129 | while (strpos($strReply, "OK") === FALSE && strpos($strReply, "ERROR") === FALSE) 130 | { 131 | $strReply = $this->readLine(); 132 | $this->strReply .= $strReply; 133 | } 134 | 135 | // return something 136 | switch (trim($strReply)) 137 | { 138 | case 'OK': 139 | return TRUE; 140 | break; 141 | 142 | case 'ERROR': 143 | return FALSE; 144 | break; 145 | 146 | default: 147 | return FALSE; 148 | break; 149 | } 150 | } 151 | 152 | // delete an sms (or multiple sms) 153 | public function delete($intMessage, $intFlag=NULL) 154 | { 155 | switch ($intFlag) 156 | { 157 | case SMS_DELETE_RECEIVED_READ: 158 | case SMS_DELETE_RECEIVED_READ_STORED_SENT: 159 | case SMS_DELETE_RECEIVED_READ_STORED: 160 | case SMS_DELETE_ALL: 161 | $strCommand = "0,{$intFlag}"; 162 | break; 163 | 164 | case SMS_DELETE_SINGLE: 165 | default: 166 | $strCommand = $intMessage; 167 | break; 168 | } 169 | 170 | // run the command 171 | return $this->command("AT+CMGD={$strCommand}"); 172 | } 173 | 174 | // get all of the recieved messages as an array 175 | public function receive() 176 | { 177 | // run the command 178 | if (!$this->command("AT+CMGL=\"ALL\"")) 179 | { 180 | return FALSE; 181 | } 182 | 183 | // get the messages as an array 184 | $arrMessages = explode("+CMGL:", $this->strReply); 185 | 186 | // remove junk from the start of the array 187 | $strJunk = array_shift($arrMessages); 188 | 189 | // set return array 190 | $arrReturn = Array(); 191 | 192 | // check that we have messages 193 | if (is_array($arrMessages) && !empty($arrMessages)) 194 | { 195 | // for each message 196 | foreach($arrMessages AS $strMessage) 197 | { 198 | // split content from metta data 199 | $arrMessage = explode("\n", $strMessage, 2); 200 | $strMetta = trim($arrMessage[0]); 201 | $arrMetta = explode(",", $strMetta); 202 | $strContent = trim($arrMessage[0]); 203 | 204 | /* metta data format is: 205 | * 0 Id 206 | * 1 Status 207 | * 2 From 208 | * 3 ? 209 | * 4 date 210 | * 5 time (with +offset) 211 | * 212 | * values may have leading, trailing (or both) double quotes 213 | */ 214 | 215 | // set the message array to go in the return array 216 | $arrReturnMessage = Array(); 217 | 218 | // set the message values to return 219 | $arrReturnMessage['Id'] = trim($arrMetta[0], "\""); 220 | $arrReturnMessage['Status'] = trim($arrMetta[1], "\""); 221 | $arrReturnMessage['From'] = trim($arrMetta[2], "\""); 222 | $arrReturnMessage['Date'] = trim($arrMetta[4], "\""); 223 | $arrTime = explode("+", $arrMetta[5], 2); 224 | $arrReturnMessage['Time'] = trim($arrTime[0], "\""); 225 | $arrReturnMessage['Content'] = trim($strContent); 226 | 227 | // add message to return array 228 | $arrReturn[] = $arrReturnMessage; 229 | } 230 | } 231 | 232 | // return messages array 233 | return $arrReturn; 234 | } 235 | 236 | // send an sms (and delete it from storage after sending) 237 | public function send($strTarget, $strMessage) 238 | { 239 | // set message id to zero 240 | $intMessage = 0; 241 | 242 | // clear response 243 | $strReply = ""; 244 | 245 | // set recipient 246 | $strCommand = "AT+CMGW=\"{$strTarget}\""; 247 | if (!$this->write($strCommand)) 248 | { 249 | return FALSE; 250 | } 251 | 252 | // get response (wait for a matching response line) 253 | while (trim($strReply) != trim($strCommand)) 254 | { 255 | $strReply = $this->readLine(); 256 | } 257 | 258 | // set message 259 | dio_write($this->_refPort, $strMessage); 260 | 261 | // send ctrl-z 262 | dio_write($this->_refPort, chr(26)); 263 | 264 | // get reply 265 | while (strpos($strReply, "OK") === FALSE && strpos($strReply, "ERROR") === FALSE) 266 | { 267 | $strReply = $this->readLine(); 268 | if (strpos($strReply, "CMGW:") !== FALSE) 269 | { 270 | $arrReply = explode(":", $strReply); 271 | $intMessage = (int)$arrReply[1]; 272 | } 273 | } 274 | 275 | // send the message 276 | if ($intMessage) 277 | { 278 | if ($this->command("AT+CMSS={$intMessage}") == TRUE) 279 | { 280 | // delete message 281 | $this->command("AT+CMGD={$intMessage}"); 282 | return TRUE; 283 | } 284 | 285 | // delete message 286 | $this->command("AT+CMGD={$intMessage}"); 287 | } 288 | 289 | return FALSE; 290 | } 291 | } 292 | 293 | 294 | 295 | ?> 296 | -------------------------------------------------------------------------------- /sms.test.php: -------------------------------------------------------------------------------- 1 | intStatus == SMS_STATUS_OK) 11 | { 12 | echo "init ok\n"; 13 | } 14 | else 15 | { 16 | echo "could not init\n"; 17 | die(); 18 | } 19 | 20 | if ($objSms->command('AT') === TRUE) 21 | { 22 | echo "at:OK\n"; 23 | } 24 | else 25 | { 26 | echo "at:ERROR\n"; 27 | } 28 | 29 | if ($objSms->send($strNumber, "this is a test message") === TRUE) 30 | { 31 | echo "send:OK\n"; 32 | } 33 | else 34 | { 35 | echo "send:ERROR\n"; 36 | } 37 | 38 | ?> 39 | --------------------------------------------------------------------------------