admin 发表于 2023-3-12 15:09:36

一个微信公众号接入多个后台服务器接口的方法

<p style="box-sizing:border-box;margin-top:0px;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">我们都知道,微信公众号的后台是只能配置一个API对接接口的。</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);"></p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">这样当一个公众号对接多个系统的时候,就没办法满足需求。</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">遇到这种情况我们如何是好?当然如果注册微信开放平台是可以实现对接多个服务的,但是这样需要开发一个微信服务平台,很多场景下不能这样做。</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">在经过调研之后,我们提出了一种可以通过转发策略来实现一个微信公众号对接多个系统的办法。</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">大致思路是:</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">微信公众号服务平台 ↔ 主对接接口 →  对接接口A、对接接口B</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">可以通过付下代码实现,将多个对接的API接口传入 wechat_api_proxy 函数,可以将服务器收到的推送消息再次原样转发给多个服务器,并且记录返回结果。</p>

<pre class="brush:php;toolbar:false language-php">function wechat_api_proxy($urls){
    foreach($urls as $url){
        $request = app('request');
        $post = $request->getContent();
        $url = $item['url'] . '?' . http_build_query($_GET);
        $header = [];
        $header['content-type'] = \ModStart\Core\Input\Request::headerGet('content-type');
        $ret = CurlUtil::postRaw($url, $post, [
            'header' => $header
        ]);
        Log::info('WechatTransfer'
            . ' -> item - ' . json_encode($item, JSON_UNESCAPED_UNICODE)
            . ' -> url - ' . $url
            . ' -> header - ' . json_encode($header, JSON_UNESCAPED_UNICODE)
            . ' -> post - ' . json_encode($post, JSON_UNESCAPED_UNICODE)
            . ' -> result - ' . json_encode($ret, JSON_UNESCAPED_UNICODE)
        );
        
        if (strtolower($ret) == 'success') {
            continue;
        }
        $results[] = $ret;
    }
    return $results;}</pre>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);"><br></p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">最终在用户返回过程中根据多个系统反馈的结果决策实际需要使用的结果。</p>

<p style="box-sizing:border-box;margin-top:0.5em;margin-bottom:0.5em;padding:0px;line-height:1.8;word-spacing:0.05rem;color:rgb(52,73,94);font-family:'Helvetica Neue', Helvetica, 'PingFang SC', Tahoma, Arial, sans-serif;font-size:13px;white-space:normal;background-color:rgb(255,255,255);">目前这种方法已经在 ModStart 多套系统中得到生产验证使用,整体还比较稳定,希望给大家提供一种思路和方法。</p><hr>以下是回复内容:<br><div class="ub-html content">
                                                    <p>你好 系统里再哪里配置???</p>
                                                </div><hr><br>采集网址:<br>https://modstart.com/thread/834
页: [1]
查看完整版本: 一个微信公众号接入多个后台服务器接口的方法