PHP

首页 -  PHP  -  微信第三方平台component_verify_ticket解密

微信第三方平台component_verify_ticket解密

php微信第三方平台component_verify_ticket解密,component_verify_ticket


我这是laravel5.5的案例

一、地址

//微信第三方授权 注意如果你是laravel请在VerifyCsrfToken文件里配置地址要不然腾讯就会访问失败
Route::any('wx/verify_ticket', 'WxTicketController@verifyTicket');//微信第三方推荐verify_ticket地址

二、方法

/**
 * @param Request $request
 * 微信每隔10分钟强求一次
 */
public function verifyTicket( Request $request )
{
    //配置
    $encodingAesKey = config('wxconfig.encodingAesKey');
    $token = config('wxconfig.token');
    $appId = config('wxconfig.appId');

    $data = $request->all();
    $timeStamp    = $data['timestamp'];
    $nonce        = $data['nonce'];
    $msg_sign     = $data['msg_signature'];
    $encryptMsg   = file_get_contents('php://input');

    $pc = new \WXBizMsgCrypt( $token, $encodingAesKey, $appId );
    $xml_tree = new \DOMDocument();
    $xml_tree->loadXML($encryptMsg);
    $array_e = $xml_tree->getElementsByTagName('Encrypt');
    $encrypt = $array_e->item(0)->nodeValue;

    $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
    $from_xml = sprintf($format, $encrypt);

    // 第三方收到公众号平台发送的消息
    $msg = '';
    $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
    if ($errCode == 0)
    {
        $xml = new \DOMDocument();
        $xml->loadXML($msg);
        $array_type = $xml->getElementsByTagName('InfoType');
        $InfoType = $array_type->item(0)->nodeValue;
        switch ( $InfoType )
        {
            case 'component_verify_ticket': //请求ticket
                $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
                $component_verify_ticket = $array_e->item(0)->nodeValue;
                //保存
                Cache::put('ticket',$component_verify_ticket,11);
                break;
            case 'unauthorized':   //取消授权
                $array_appid = $xml->getElementsByTagName('AuthorizerAppid');
                $authorizer_appid = $array_appid->item(0)->nodeValue;
                //这里写你的业务
                break;
            case 'updateauthorized'://更新授权
                $array_code = $xml->getElementsByTagName('AuthorizationCode');
                $code = $array_code->item(0)->nodeValue;
                //授权code 这里写你的业务
                break;
            default:
                echo "false"; die();
                break;
        }
        echo 'success';
    } else
    {
        echo "false";
    }
}

三、解密类下载WXBizMsgCrypt

链接: https://pan.baidu.com/s/1_YSOvsQXLoJC4qgNvRKEMA 密码: pp5w

(36)
分享:

本文由:xiaoshu168 作者:xiaoshu发表,转载请注明来源!

相关阅读