Home > Article > Backend Development > PHP sends AT command example code, phpat command example_PHP tutorial
Requirements: Send text messages to the user’s mobile phone, requiring that the message content can be customized
Problem: There is no telecom mode, and the free api interface cannot be used to customize the text message content
Solution: Connect to the server through 4G network card, operate the network card through AT commands, and send text messages
After checking, it was found that AT quality transmission requires multiple encoding of the information, and the transmission timed out, but the actual transmission was successful, and the problem has not been completely solved
The code is as follows:
<?php send_message('', '您好, 你正在申请注册服务平台, 您的验证码是: '); function send_message($phone, $message) { $phone = decode_phone($phone); $message_center = decode_message_center('+'); //此步骤似乎没用 $message = decode_message($message); $length = get_message_length($phone, $message); set_time_limit(); //根据情况选择COM口, 此处为COM, 在管理设备中, 查看网络解调器的COM口 exec('mode COM: baud= data= stop= parity=n xon=on'); $fd = dio_open('COM:', O_RDWR); $ff = dio_stat($fd); if (!$fd) { die("failed open com"); } //chr()是回车, chr()是Ctrl+Z dio_write($fd, "AT+CMGF=" . chr()); //设置发送模式为PDU dio_write($fd, "AT+CMGS=" . $length . chr()); //发送信息长度 dio_write($fd, 'D' . $phone . '' . $message . chr() . chr()); //发送短信息 sleep(); dio_close($fd); } function decode_phone($phone) { $phone = str_replace('+', '', $phone); if (strlen($phone) % !== ) { $phone = $phone . 'F'; } $newPhone = ''; for ($i = ; $i < strlen($phone); $i += ) { $newPhone = $newPhone . substr($phone, $i + , ) . substr($phone, $i, ); } return $newPhone; } function decode_message_center($phone) { $newPhone = '' . decode_phone($phone); $len = strtoupper(dechex(strlen($newPhone) / )); if (strlen($len) % !== ) { $len = '' . $len; } $newPhone = $len . $newPhone; return $newPhone; } function decode_message($message) { $newMessage = utf_unicode($message); $len = strtoupper(dechex(strlen($newMessage) / )); if (strlen($len) % !== ) { $len = '' . $len; } $newMessage = $len . $newMessage; return $newMessage; } function utf_unicode($name) { $name = iconv('UTF-', 'UCS-', $name); $len = strlen($name); $str = ''; for ($i = ; $i < $len - ; $i = $i + ) { $c = $name[$i]; $c = $name[$i + ]; if (ord($c) > ) { //两个字节的文字 $str .= base_convert(ord($c), , ) . str_pad(base_convert(ord($c), , ), , , STR_PAD_LEFT); } else { $str .= str_pad(base_convert(ord($c), , ), , , STR_PAD_LEFT); } } $str = strtoupper($str); //转换为大写 return $str; } function get_message_length($phone, $message) { $str = 'D' . $phone . '' . $message; $length = strlen($str) / ; if ($length < ) { $length = '' . $length; } return $length; }
The above is the example code for sending AT commands in PHP introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Bangkejia website!