折腾了几天,终于解决:微信公众号第三方平台 获取component_verify_ticket

时间:2015-07-06 12:29:58   收藏:0   阅读:489

直接上干货,贴出代码:

<?php


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 = "<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) {

    //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= ‘insert into cjapp_weixin_account (appId,token,encodingAesKey,component_verify_ticket) VALUES (‘.$appId.‘,‘.$token.‘,‘.$encodingAesKey.‘,‘.$component_verify_ticket.‘)‘;

    // $sql= "insert into cjapp_weixin_account (token,appId) VALUES ($token,$appId)";

    $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 ;











































本文出自 “枷罗博客” 博客,请务必保留此出处http://jhomephper.blog.51cto.com/8558055/1671235

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!