首頁 >後端開發 >php教程 >PHP发送AT指令,phpat指令_PHP教程

PHP发送AT指令,phpat指令_PHP教程

WBOY
WBOY原創
2016-07-12 08:51:36833瀏覽

PHP发送AT指令,phpat指令

需求: 发送短信到用户输入手机, 要求可以自定义信息内容

问题: 没有电信猫, 使用免费api接口无法自定义短信内容

解决方案: 通过4G网卡, 接在服务器上, 通过AT指令操作网卡, 发送短信

查阅发现, AT质量发送需要对信息进行多重编码, 而且发送超时, 但实际发送成功, 问题还未完全解决

代码如下: 

<span>  1</span> <?<span>php
</span><span>  2</span> send_message('8613800138000', '您好, 你正在申请注册服务平台, 您的验证码是: 97431'<span>);
</span><span>  3</span>  
<span>  4</span> <span>function</span> send_message(<span>$phone</span>, <span>$message</span><span>) {
</span><span>  5</span>     <span>$phone</span> = decode_phone(<span>$phone</span><span>);
</span><span>  6</span>      
<span>  7</span>     <span>$message_center</span> = decode_message_center('+8613010200500'); <span>//</span><span>此步骤似乎没用</span>
<span>  8</span>      
<span>  9</span>     <span>$message</span> = decode_message(<span>$message</span><span>);
</span><span> 10</span>      
<span> 11</span>     <span>$length</span> = get_message_length(<span>$phone</span>, <span>$message</span><span>);
</span><span> 12</span>      
<span> 13</span>     <span>set_time_limit</span>(0<span>);
</span><span> 14</span>      
<span> 15</span>     <span>//</span><span>根据情况选择COM口, 此处为COM4, 在管理设备中, 查看网络解调器的COM口</span>
<span> 16</span> 
<span> 17</span>     <span>exec</span>('mode COM4: baud=115200 data=8 stop=1 parity=n xon=on'<span>);
</span><span> 18</span>      
<span> 19</span>     <span>$fd</span> = dio_open('COM4:',<span> O_RDWR);
</span><span> 20</span>      
<span> 21</span>     <span>$ff</span> = dio_stat(<span>$fd</span><span>);
</span><span> 22</span>      
<span> 23</span>     <span>if</span> (!<span>$fd</span><span>) {
</span><span> 24</span>         <span>die</span>("failed open com4"<span>);
</span><span> 25</span> <span>    }
</span><span> 26</span>      
<span> 27</span>     <span>//</span><span>chr(13)是回车, chr(16)是Ctrl+Z</span>
<span> 28</span>      
<span> 29</span>     dio_write(<span>$fd</span>, "AT+CMGF=0" . <span>chr</span>(13)); <span>//</span><span>设置发送模式为PDU</span>
<span> 30</span>      
<span> 31</span>     dio_write(<span>$fd</span>, "AT+CMGS=" . <span>$length</span> . <span>chr</span>(13)); <span>//</span><span>发送信息长度</span>
<span> 32</span>      
<span> 33</span>     dio_write(<span>$fd</span>, '11000D91' . <span>$phone</span> . '000800' . <span>$message</span> . <span>chr</span>(26) . <span>chr</span>(13)); <span>//</span><span>发送短信息</span>
<span> 34</span>      
<span> 35</span>     <span>sleep</span>(2<span>);
</span><span> 36</span>      
<span> 37</span>     dio_close(<span>$fd</span><span>);
</span><span> 38</span> <span>}
</span><span> 39</span>  
<span> 40</span> <span>function</span> decode_phone(<span>$phone</span><span>) {
</span><span> 41</span>     <span>$phone</span> = <span>str_replace</span>('+', '', <span>$phone</span><span>);
</span><span> 42</span>      
<span> 43</span>     <span>if</span> (<span>strlen</span>(<span>$phone</span>) % 2 !== 0<span>) {
</span><span> 44</span>         <span>$phone</span> = <span>$phone</span> . 'F'<span>;
</span><span> 45</span> <span>    }
</span><span> 46</span>      
<span> 47</span>     <span>$newPhone</span> = ''<span>;
</span><span> 48</span>     <span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>strlen</span>(<span>$phone</span>); <span>$i</span> += 2<span>) {
</span><span> 49</span>         <span>$newPhone</span> = <span>$newPhone</span> . <span>substr</span>(<span>$phone</span>, <span>$i</span> + 1, 1) . <span>substr</span>(<span>$phone</span>, <span>$i</span>, 1<span>);
</span><span> 50</span> <span>    }
</span><span> 51</span>      
<span> 52</span>     <span>return</span> <span>$newPhone</span><span>;
</span><span> 53</span> <span>}
</span><span> 54</span>  
<span> 55</span> <span>function</span> decode_message_center(<span>$phone</span><span>) {
</span><span> 56</span>     <span>$newPhone</span> = '91' . decode_phone(<span>$phone</span><span>);
</span><span> 57</span>      
<span> 58</span>     <span>$len</span> = <span>strtoupper</span>(<span>dechex</span>(<span>strlen</span>(<span>$newPhone</span>) / 2<span>));
</span><span> 59</span>      
<span> 60</span>     <span>if</span> (<span>strlen</span>(<span>$len</span>) % 2 !== 0<span>) {
</span><span> 61</span>         <span>$len</span> = '0' . <span>$len</span><span>;
</span><span> 62</span> <span>    }
</span><span> 63</span>      
<span> 64</span>     <span>$newPhone</span> = <span>$len</span> . <span>$newPhone</span><span>;
</span><span> 65</span>      
<span> 66</span>     <span>return</span> <span>$newPhone</span><span>;
</span><span> 67</span> <span>}
</span><span> 68</span>  
<span> 69</span> <span>function</span> decode_message(<span>$message</span><span>) {
</span><span> 70</span>     <span>$newMessage</span> = utf8_unicode(<span>$message</span><span>);
</span><span> 71</span>      
<span> 72</span>     <span>$len</span> = <span>strtoupper</span>(<span>dechex</span>(<span>strlen</span>(<span>$newMessage</span>) / 2<span>));
</span><span> 73</span>      
<span> 74</span>     <span>if</span> (<span>strlen</span>(<span>$len</span>) % 2 !== 0<span>) {
</span><span> 75</span>         <span>$len</span> = '0' . <span>$len</span><span>;
</span><span> 76</span> <span>    }
</span><span> 77</span>      
<span> 78</span>     <span>$newMessage</span> = <span>$len</span> . <span>$newMessage</span><span>;
</span><span> 79</span>      
<span> 80</span>     <span>return</span> <span>$newMessage</span><span>;
</span><span> 81</span> <span>}
</span><span> 82</span>  
<span> 83</span> <span>function</span> utf8_unicode(<span>$name</span><span>) {
</span><span> 84</span>     <span>$name</span> = <span>iconv</span>('UTF-8', 'UCS-2', <span>$name</span><span>);
</span><span> 85</span>     <span>$len</span> = <span>strlen</span>(<span>$name</span><span>);
</span><span> 86</span>     <span>$str</span> = ''<span>;
</span><span> 87</span>      
<span> 88</span>     <span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$len</span> - 1; <span>$i</span> = <span>$i</span> + 2<span>) {
</span><span> 89</span>         <span>$c</span> = <span>$name</span>[<span>$i</span><span>];
</span><span> 90</span>         <span>$c2</span> = <span>$name</span>[<span>$i</span> + 1<span>];
</span><span> 91</span>         <span>if</span> (<span>ord</span>(<span>$c</span>) > 0) { <span>//</span><span>两个字节的文字  </span>
<span> 92</span>             <span>$str</span> .= <span>base_convert</span>(<span>ord</span>(<span>$c</span>), 10, 16) . <span>str_pad</span>(<span>base_convert</span>(<span>ord</span>(<span>$c2</span>), 10, 16), 2, 0,<span> STR_PAD_LEFT);
</span><span> 93</span>         } <span>else</span><span> {
</span><span> 94</span>             <span>$str</span> .= <span>str_pad</span>(<span>base_convert</span>(<span>ord</span>(<span>$c2</span>), 10, 16), 4, 0,<span> STR_PAD_LEFT);
</span><span> 95</span> <span>        }
</span><span> 96</span> <span>    }
</span><span> 97</span>      
<span> 98</span>     <span>$str</span> = <span>strtoupper</span>(<span>$str</span>); <span>//</span><span>转换为大写  </span>
<span> 99</span>     <span>return</span> <span>$str</span><span>;
</span><span>100</span> <span>}
</span><span>101</span>  
<span>102</span> <span>function</span> get_message_length(<span>$phone</span>, <span>$message</span><span>) {
</span><span>103</span>     <span>$str</span> = '11000D91' . <span>$phone</span> . '000800' . <span>$message</span><span>;
</span><span>104</span>      
<span>105</span>     <span>$length</span> = <span>strlen</span>(<span>$str</span>) / 2<span>;
</span><span>106</span>      
<span>107</span>     <span>if</span> (<span>$length</span> < 10<span>) {
</span><span>108</span>         <span>$length</span> = '0' . <span>$length</span><span>;
</span><span>109</span> <span>    }
</span><span>110</span>      
<span>111</span>     <span>return</span> <span>$length</span><span>;
</span><span>112</span> }

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1129314.htmlTechArticlePHP发送AT指令,phpat指令 需求: 发送短信到用户输入手机, 要求可以自定义信息内容 问题: 没有电信猫, 使用免费api接口无法自定义短信内容...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn