PHP sends AT commands and phpat commands
Requirement: send the text message to the user's input mobile phone. You can customize the information.
Problem: No Telecom cat, you cannot customize text message content using the free api
Solution: connect to the server through a 4G Nic, operate the NIC through the AT command, and send text messages
It is found that AT quality transmission requires multi-encoding of information and sending timeout, but the actual sending is successful, and the problem has not been completely solved.
The Code is as follows:
1 <? Php 2 send_message ('123456', 'Hello, you are applying to register the service platform. Your verification code is: 123456'); 3 4 function send_message ($ phone, $ message) {5 $ phone = decode_phone ($ phone); 6 7 $ message_center = decode_message_center ('+ 100 '); // This step does not seem to use 8 9 $ message = decode_message ($ message); 10 11 $ length = get_message_length ($ phone, $ message); 12 13 set_time_limit (0 ); 14 15 // select the comport according to the actual situation. Here is COM4. In the management device, view the comport 16 17 ex of the network modem. Ec ('mode COM4: baud = 115200 data = 8 stop = 1 parity = n xon = On'); 18 19 $ fd = dio_open ('com4: ', O_RDWR ); 20 21 $ ff = dio_stat ($ fd); 22 23 if (! $ Fd) {24 die ("failed open com4"); 25} 26 27 // chr (13) is carriage return, chr (16) is Ctrl + Z 28 29 dio_write ($ fd, "AT + CMGF = 0 ". chr (13); // set the sending mode to PDU 30 31 dio_write ($ fd, "AT + CMGS = ". $ length. chr (13); // The length of the message to be sent is 32 33 dio_write ($ fd, '11000d91 '. $ phone. '123 '. $ message. chr (26 ). chr (13); // send Short Message 34 35 sleep (2); 36 37 dio_close ($ fd); 38} 39 40 function decode_phone ($ phone) {41 $ phone = str_replace ('+ ','', $ Phone); 42 43 if (strlen ($ phone) % 2! = 0) {44 $ phone = $ phone. 'F'; 45} 46 47 $ newPhone = ''; 48 for ($ I = 0; $ I <strlen ($ phone); $ I ++ = 2) {49 $ newPhone = $ newPhone. substr ($ phone, $ I + 1, 1 ). substr ($ phone, $ I, 1); 50} 51 52 return $ newPhone; 53} 54 55 function decode_message_center ($ phone) {56 $ newPhone = '91 '. decode_phone ($ phone); 57 58 $ len = strtoupper (dechex (strlen ($ newPhone)/2); 59 60 if (strlen ($ len) % 2! = 0) {61 $ len = '0 '. $ len; 62} 63 64 $ newPhone = $ len. $ newPhone; 65 66 return $ newPhone; 67} 68 69 function decode_message ($ message) {70 $ newMessage = utf8_unicode ($ message ); 71 72 $ len = strtoupper (dechex (strlen ($ newMessage)/2); 73 74 if (strlen ($ len) % 2! = 0) {75 $ len = '0 '. $ len; 76} 77 78 $ newMessage = $ len. $ newMessage; 79 80 return $ newMessage; 81} 82 83 function utf8_unicode ($ name) {84 $ name = iconv ('utf-8', 'ucs-2 ', $ name); 85 $ len = strlen ($ name); 86 $ str = ''; 87 88 for ($ I = 0; $ I <$ len-1; $ I = $ I + 2) {89 $ c = $ name [$ I]; 90 $ c2 = $ name [$ I + 1]; 91 if (ord ($ c)> 0) {// two bytes of text 92 $ str. = base_convert (ord ($ c), 10, 16 ). str_pad (base_convert (ord ($ c2), 10, 16), 2, 0, STR_PAD_LEFT); 93} else {94 $ str. = str_pad (base_convert (ord ($ c2), 10, 16), 4, 0, STR_PAD_LEFT); 95} 96} 97 98 $ str = strtoupper ($ str ); // convert to uppercase 99 return $ str; 100} 101 102 function get_message_length ($ phone, $ message) {103 $ str = '11000d91 '. $ phone. '123 '. $ message; 104 105 $ length = strlen ($ str)/2; 106 107 if ($ length <10) {108 $ length = '0 '. $ length; 109} 110 111 return $ length; 112}