直接上干货,贴出代码:


include_once "../Public/wxasset/php/wxBizMsgCrypt.php";

//链接数据库

$link =  mysql_connect('127.0.0.1','cjadmin','admincjapp');

$db = mysql_select_db('cjapp', $link);


//测试数据库连接

// $sql= "INSERT INTO cjapp_weixin_account (token, type) VALUES ('1342342342eweqweqw', 2)";

//     $res = mysql_query($sql);

//     if($res)

//     {

//         echo 'success';

//     }


//第三方发送消息给公众平台

$encodingAesKey = '32GAfm1MK27AQxeUzS9RQsMqyHGp0xgVXpQvjo14xT1b';

$token = 'dmme333qq1429251266';

$appId = 'wx2a86333d5926e6e7f35';

$timeStamp  = empty($_GET['timestamp'])     ? ""    : trim($_GET['timestamp']) ;

$nonce      = empty($_GET['nonce'])     ? ""    : trim($_GET['nonce']) ;

$msg_sign   = empty($_GET['msg_signature']) ? ""    : trim($_GET['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 = "";

$from_xml = sprintf($format, $encrypt);



// 第三方收到公众号平台发送的消息

$msg = '';

$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);


if ($errCode == 0) {

    //print("解密后: " . $msg . "\n");

    $xml = new DOMDocument();

    $xml->loadXML($msg);

    $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');

    $component_verify_ticket = $array_e->item(0)->nodeValue;

    // logResult('解密后的component_verify_ticket是:'.$component_verify_ticket);

   

 $sql_init = "select *  from cjapp_weixin_account where token='$token'  limit  1";

    if(mysql_query($sql_init)){

        $sql = "update cjapp_weixin_account set component_verify_ticket='$component_verify_ticket'";

    }else{

        $sql = "insert into cjapp_weixin_account (appId,token,encodingAesKey,component_verify_ticket) VALUES ('$appId','$token','$encodingAesKey','$component_verify_ticket')";

    }


    $res = mysql_query($sql);

    if($res)

    {

        echo 'success';

    }

} else {

    // logResult('解密后失败:'.$errCode);

    $sql= "insert into cjapp_weixin_account (errCode) VALUES ($errCode)";

    $res = mysql_query($sql);

    if($res)

    {

        echo 'failed';

    }

}


mysql_close($db);

die();







数据库:

CREATE TABLE IF NOT EXISTS `cjapp_weixin_account` (

  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,

  `appId` varchar(50) NOT NULL,

  `appSecret` varchar(100) NOT NULL,

  `token` varchar(100) DEFAULT NULL,

  `encodingAesKey` varchar(100) DEFAULT NULL,

  `type` tinyint(3) DEFAULT NULL COMMENT '1订阅号2服务号3开放平台网站应用4开放平台公众号服务5开放平台移动应用',

  `date_time` datetime DEFAULT NULL,

  `component_verify_ticket` varchar(100) DEFAULT NULL,

  `dateline` int(10) DEFAULT NULL,

  `errCode` varchar(200) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;