欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

微信第三方登录 - php获取component_verify_ticket?

程序员文章站 2022-05-20 14:50:03
...
public function index()
{    

    $timeStamp    ="";
    $nonce        ="";
    $encrypt_type ='';
    $msg_sign     ='';
    $encryptMsg   =file_get_contents('php://input');
    
    $result = $this->getVerify_Ticket($timeStamp,$nonce,$encrypt_type,$msg_sign,$encryptMsg);

    if($result){
        echo "success";
    }

}

//获取component_verify_ticket

public function getVerify_Ticket($timeStamp,$nonce,$encrypt_type,$msg_sign,$encryptMsg){

    $pc = new WXBizMsgCrypt(WxPayConfig::Token, WxPayConfig::EncodingAesKey, WxPayConfig::open_AppID);

    $xml_tree = new \DOMDocument();
    $xml_tree->loadXML($encryptMsg);
    $array_e = $xml_tree->getElementsByTagName('Encrypt');
    $encrypt = $array_e->item(0)->nodeValue;

    $format = "";

    $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_e = $xml->getElementsByTagName('ComponentVerifyTicket');

        $component_verify_ticket = $array_e->item(0)->nodeValue;
        DB::getDB()->delete("wechat_verifyticket",'uptime!=1');
        DB::getDB()->insert("wechat_verifyticket",array(
            'component_verify_ticket'    => $component_verify_ticket,
            'uptime'                    => time()));

        return true;
    }else{
        DB::getDB()->delete("wechat_verifyticket",'uptime!=1');
        DB::getDB()->insert("wechat_verifyticket",array(
            'component_verify_ticket'    => $errCode,
            'uptime'                    => time()));
        return false;
    }

} 

回复内容:

public function index()

{    

    $timeStamp    ="";
    $nonce        ="";
    $encrypt_type ='';
    $msg_sign     ='';
    $encryptMsg   =file_get_contents('php://input');
    
    $result = $this->getVerify_Ticket($timeStamp,$nonce,$encrypt_type,$msg_sign,$encryptMsg);

    if($result){
        echo "success";
    }

}

//获取component_verify_ticket

public function getVerify_Ticket($timeStamp,$nonce,$encrypt_type,$msg_sign,$encryptMsg){

    $pc = new WXBizMsgCrypt(WxPayConfig::Token, WxPayConfig::EncodingAesKey, WxPayConfig::open_AppID);

    $xml_tree = new \DOMDocument();
    $xml_tree->loadXML($encryptMsg);
    $array_e = $xml_tree->getElementsByTagName('Encrypt');
    $encrypt = $array_e->item(0)->nodeValue;

    $format = "";

    $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_e = $xml->getElementsByTagName('ComponentVerifyTicket');

        $component_verify_ticket = $array_e->item(0)->nodeValue;
        DB::getDB()->delete("wechat_verifyticket",'uptime!=1');
        DB::getDB()->insert("wechat_verifyticket",array(
            'component_verify_ticket'    => $component_verify_ticket,
            'uptime'                    => time()));

        return true;
    }else{
        DB::getDB()->delete("wechat_verifyticket",'uptime!=1');
        DB::getDB()->insert("wechat_verifyticket",array(
            'component_verify_ticket'    => $errCode,
            'uptime'                    => time()));
        return false;
    }

} 

出错提示是什么?