Home  >  Article  >  Backend Development  >  Methods and example codes for sending AT commands in PHP

Methods and example codes for sending AT commands in PHP

墨辰丷
墨辰丷Original
2018-06-02 10:02:071589browse

This article mainly introduces the relevant information about the example code of PHP sending AT command. It is very good and has reference value. Friends who need it can take a look.

Requirement: Send SMS to The user enters his mobile phone and is required to customize the message content

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 a 4G network card, operate the network card through AT commands, and send text messages

After checking, it was found that AT quality sending requires multiple encoding of the information, and the sending times out. , but the actual sending was successful, the problem has not been completely solved

The code is as follows:

<?php
send_message(&#39;&#39;, &#39;您好, 你正在申请注册服务平台, 您的验证码是: &#39;);
function send_message($phone, $message) {
$phone = decode_phone($phone);
$message_center = decode_message_center(&#39;+&#39;); //此步骤似乎没用
$message = decode_message($message);
$length = get_message_length($phone, $message);
set_time_limit();
//根据情况选择COM口, 此处为COM, 在管理设备中, 查看网络解调器的COM口
exec(&#39;mode COM: baud= data= stop= parity=n xon=on&#39;);
$fd = dio_open(&#39;COM:&#39;, 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, &#39;D&#39; . $phone . &#39;&#39; . $message . chr() . chr()); //发送短信息
sleep();
dio_close($fd);
}
function decode_phone($phone) {
$phone = str_replace(&#39;+&#39;, &#39;&#39;, $phone);
if (strlen($phone) % !== ) {
$phone = $phone . &#39;F&#39;;
}
$newPhone = &#39;&#39;;
for ($i = ; $i < strlen($phone); $i += ) {
$newPhone = $newPhone . substr($phone, $i + , ) . substr($phone, $i, );
}
return $newPhone;
}
function decode_message_center($phone) {
$newPhone = &#39;&#39; . decode_phone($phone);
$len = strtoupper(dechex(strlen($newPhone) / ));
if (strlen($len) % !== ) {
$len = &#39;&#39; . $len;
}
$newPhone = $len . $newPhone;
return $newPhone;
}
function decode_message($message) {
$newMessage = utf_unicode($message);
$len = strtoupper(dechex(strlen($newMessage) / ));
if (strlen($len) % !== ) {
$len = &#39;&#39; . $len;
}
$newMessage = $len . $newMessage;
return $newMessage;
}
function utf_unicode($name) {
$name = iconv(&#39;UTF-&#39;, &#39;UCS-&#39;, $name);
$len = strlen($name);
$str = &#39;&#39;;
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 = &#39;D&#39; . $phone . &#39;&#39; . $message;
$length = strlen($str) / ;
if ($length < ) {
$length = &#39;&#39; . $length;
}
return $length;
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP Learning Record Array Function

PHP Implementation of Exporting Styled Excel Example Sharing

PHP implements the method of forcing the specified callback type based on Callable

The above is the detailed content of Methods and example codes for sending AT commands in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn