ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 通讯内容 # 两种通讯方式 ## 第一种token方式 ##### 访问范围 > `controllers\Api`目录下的接口 ##### 访问必带参数 > `token` ##### token验证方式 > 过滤请求中参数值等于null的参数,合并GET、POST请求参数 ``` <pre class="calibre14">``` $data <span class="token">=</span> $this<span class="token">-</span><span class="token">></span><span class="token3">_array_merge</span><span class="token1">(</span>$_GET <span class="token1">,</span> $_POST<span class="token1">)</span><span class="token1">;</span> protected <span class="token2">function</span> <span class="token3">_array_merge</span><span class="token1">(</span>$a<span class="token1">,</span>$b<span class="token1">)</span><span class="token1">{</span> $r <span class="token">=</span> $a<span class="token">+</span>$b<span class="token1">;</span> <span class="token3">foreach</span><span class="token1">(</span>$r as $k<span class="token">=</span><span class="token">></span>$v<span class="token1">)</span><span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span><span class="token3">is_null</span><span class="token1">(</span>$v<span class="token1">)</span><span class="token1">)</span><span class="token1">{</span> $r<span class="token1">[</span>$k<span class="token1">]</span> <span class="token">=</span> $b<span class="token1">[</span>$k<span class="token1">]</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token2">return</span> $r<span class="token1">;</span> ``` ``` > 删除数组中的ctl、met、typ参数 ``` <pre class="calibre14">``` <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span><span class="token3">isset</span><span class="token1">(</span>$_POST<span class="token1">[</span><span class="token4">'ctl'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">unset</span><span class="token1">(</span>$data<span class="token1">[</span><span class="token4">'ctl'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span><span class="token3">isset</span><span class="token1">(</span>$_POST<span class="token1">[</span><span class="token4">'met'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">unset</span><span class="token1">(</span>$data<span class="token1">[</span><span class="token4">'met'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span><span class="token3">isset</span><span class="token1">(</span>$_POST<span class="token1">[</span><span class="token4">'typ'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token3">unset</span><span class="token1">(</span>$data<span class="token1">[</span><span class="token4">'typ'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` > 使用libraries/**init**.php 文件下 > `check_url_with_encrypt($key, $formvars=array())`函数进行验证 > 其中$key为对应的系统秘钥,$formvars为处理后的数组 ``` <pre class="calibre14">``` <span class="token2">function</span> <span class="token3">check_url_with_encrypt</span><span class="token1">(</span>$key<span class="token1">,</span> $formvars<span class="token">=</span><span class="token3">array</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> $token <span class="token">=</span> $formvars<span class="token1">[</span><span class="token4">'token'</span><span class="token1">]</span><span class="token1">;</span> <span class="token3">unset</span><span class="token1">(</span>$formvars<span class="token1">[</span><span class="token4">'token'</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> $hash_row <span class="token">=</span> $formvars<span class="token1">;</span> <span class="token3">array_multiksort</span><span class="token1">(</span>$hash_row<span class="token1">,</span> SORT_STRING<span class="token1">)</span><span class="token1">;</span> $hash_row<span class="token1">[</span><span class="token4">'key'</span><span class="token1">]</span> <span class="token">=</span> $key<span class="token1">;</span> $tmp_str <span class="token">=</span> <span class="token3">http_build_query</span><span class="token1">(</span>$hash_row<span class="token1">)</span><span class="token1">;</span> <span class="token5">//可以判断请求时间是否超过某个期限, 1分钟内</span> <span class="token2">if</span> <span class="token1">(</span><span class="token1">(</span><span class="token3">get_time</span><span class="token1">(</span><span class="token1">)</span> <span class="token">-</span> $hash_row<span class="token1">[</span><span class="token4">'rtime'</span><span class="token1">]</span> <span class="token"><</span> <span class="token6">60</span><span class="token1">)</span> <span class="token">&&</span> $token <span class="token">==</span> <span class="token3">md5</span><span class="token1">(</span>$tmp_str<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token6">true</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token6">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` > 注意:如果服务器时间不统一,有可能会出现请求超时,建议使用前同步服务器时间 ## 第二种u和k方式 ##### 访问范围 > 非`controllers\Api`目录下的接口 ##### 访问必带参数 > `u` 、`k` 、`typ=json` ##### `u`、`k`生成方式 *该方式有问题,计划修正中*