首頁 > php教程 > php手册 > PHP发送AT指令

PHP发送AT指令

WBOY
發布: 2016-05-27 08:46:44
原創
1231 人瀏覽過

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

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

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

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

代码如下: 

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

 

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板