php对接微信客服,openapi回调地址请求不通过报错解决

直接上代码

class WechatService extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    private $token;
    private $encodingAesKey;
    private $appId;

    public function __construct($token = "前端设置的token", $encodingAesKey = "前端设置的encodingAesKey", $appId = "企业微信的corpid")
    {
        $this->token = $token;
        $this->encodingAesKey = base64_decode($encodingAesKey . '=');
        $this->appId = $appId;
    }

    /**
     * 回调地址
     */
    public function callback() {
        file_put_contents('wechat1.log',json_encode($_GET));
        $xml = $this->decryptMsg($_GET['echostr']);
        echo $xml;
    }

    public function decryptMsg($encryptMsg)
    {
        // 对密文进行解密
        $ciphertext_dec = base64_decode($encryptMsg);
        $iv = substr($this->encodingAesKey, 0, 16);
        $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->encodingAesKey, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

        // 去除补位字符
        $result = $this->decode($decrypted);

        // 去除16位随机字符串, 网络字节序
        if (strlen($result) < 16) {
            return "";
        }
        $content = substr($result, 16, strlen($result));
        $len_list = unpack("N", substr($content, 0, 4));
        $xml_len = $len_list[1];
        $xml_content = substr($content, 4, $xml_len);
        $from_appid = substr($content, $xml_len + 4);

        if ($from_appid != $this->appId) {
            return "";
        }
        return $xml_content;
    }

    private function decode($text)
    {
        $pad = ord(substr($text, -1));
        if ($pad < 1 || $pad > 32) {
            $pad = 0;
        }
        return substr($text, 0, (strlen($text) - $pad));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值