微信开放平台开发-授权、全网发布(PHP)

这两天做了微信开发平台的开发,梳理下。。。
浙江百牛信息技术bainiu.ltd整理发布于博客园

先看看授权的流程:

第一步:接收component_verify_ticket:
1、微信服务器每隔10分钟会向第三方的消息接收地址推送一次component_verify_ticket,拿到后需要在本地做好存储;
2、微信第三方平台的消息是加密的(下图),需要进行解密才能获取需要的信息;
3、接收并解密消息,代码如下:
[php] view plain copy
 
  1. $timeStamp = empty ( $_GET ['timestamp'] ) ? '' : trim ( $_GET ['timestamp'] );  
  2. $nonce = empty ( $_GET ['nonce'] ) ? '' : trim ( $_GET ['nonce'] );  
  3. $msg_sign = empty ( $_GET ['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] );  
  4. $encryptMsg = file_get_contents ( 'php://input' );  
  5. $pc = new WXBizMsgCrypt ( OPEN_MSG_VERIFY_TOKEN, OPEN_ENCRYPT_KEY, OPEN_APPID );  
  6. $postArr = ArrayUtil::xml2array ( $encryptMsg ); // xml对象解析  
  7. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";  
  8. $from_xml = sprintf ( $format, $postArr ['Encrypt'] );  
  9. // 第三方收到公众号平台发送的消息  
  10. $msg = '';  
  11. $errCode = $pc->decryptMsg ( $msg_sign, $timeStamp, $nonce, $from_xml, $msg ); // 解密  
  12. if ($errCode == 0) {  
  13.     $param = ArrayUtil::xml2array ( $msg );  
  14.     switch ($param ['InfoType']) {  
  15.         case 'component_verify_ticket' : // 授权凭证  
  16.             $component_verify_ticket = $param ['ComponentVerifyTicket'];  
  17.             $ret ['component_verify_ticket'] = $component_verify_ticket;  
  18.             file_put_contents ( OPEN_COMPONENT_VERIFY_TICKET_PATH, $component_verify_ticket ); // 缓存  
  19.             break;  
  20.         case 'unauthorized' : // 取消授权  
  21.             $status = 2;  
  22.             break;  
  23.         case 'authorized' : // 授权  
  24.             $status = 1;  
  25.             break;  
  26.         case 'updateauthorized' : // 更新授权  
  27.             break;  
  28.     }  
  29. }  
第二步:获取component_access_token:
每个令牌是存在有效期(2小时)的,且令牌的调用不是无限制的,请第三方平台做好令牌的管理,在令牌快过期时(比如1小时50分)再进行刷新。所以要对component_access_token做好本地缓存,代码如下:
[php] view plain copy
 
  1. $nowTime = time ();  
  2. $getCache = false; // 是否从缓存获取  
  3. if (is_file ( OPEN_COMPONENT_ACCESS_TOKEN_PATH )) {  
  4.     $cacheJson = file_get_contents ( OPEN_COMPONENT_ACCESS_TOKEN_PATH );  
  5.     if (json_decode ( $cacheJson )) {  
  6.         $cacheArr = json_decode ( $cacheJson, true );  
  7.         if (! empty ( $cacheArr ['component_access_token'] ) && $cacheArr ['expires_in'] > $nowTime + 120) {  
  8.             $ret ['errCode'] = 0;  
  9.             $ret ['component_access_token'] = $cacheArr ['component_access_token'];  
  10.             $getCache = true;  
  11.         }  
  12.     }  
  13. }  
  14. // 若本地缓存不存在或已失效,则从微信服务器重新获取  
  15. if (! $getCache) {  
  16.     $url = WeixinApiLang::$url ['component_token'];  
  17.     $ticket = file_get_contents ( OPEN_COMPONENT_VERIFY_TICKET_PATH );  
  18.     $data = '{  
  19.             "component_appid":"' . OPEN_APPID . '",  
  20.             "component_appsecret":"' . OPEN_APPSECRET . '",  
  21.             "component_verify_ticket":"' . $ticket . '"  
  22.     }';  
  23.     $ret = self::requestWeixinAPI ( $url, $data );  
  24.     // 缓存到本地  
  25.     if (isset ( $ret ['component_access_token'] )) {  
  26.         $cacheArr = array ();  
  27.         $cacheArr ['component_access_token'] = $ret ['component_access_token'];  
  28.         $ret ['expires_in'] += $nowTime;  
  29.         $cacheArr ['expires_in'] = $ret ['expires_in'];  
  30.         file_put_contents ( OPEN_COMPONENT_ACCESS_TOKEN_PATH, json_encode ( $cacheArr ) );  
  31.     }  
  32. }  
第三步:获取pre_auth_code(注意这是预授权码,不是授权码):
[php] view plain copy
 
  1. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $accessToken;  
  2. $data = '{  
  3.     "component_appid":"' . OPEN_APPID . '"  
  4. }';  
  5. $ret = self::requestWeixinAPI ( $url, $data );  
第四步:使用授权码换取公众号的接口调用凭据和授权信息:
授权码authorization_code是用户“确认授权”后,微信回调时返回的。代码如下:
[php] view plain copy
 
  1. $url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $accessToken;  
  2. $data = '{  
  3.         "component_appid":"' . OPEN_APPID . '",  
  4.         "authorization_code": "' . $authorization_code . '"  
  5. }';  
  6. $ret = self::requestWeixinAPI ( $url, $data );  
到此,授权流程就走完了。。。

接着看看全网发布的测试用例怎么做:

1、模拟粉丝触发专用测试公众号的事件,并推送事件消息到专用测试公众号,第三方平台方开发者需要提取推送XML信息中的event值,并在5秒内立即返回按照下述要求组装的文本消息给粉丝;
2、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需根据文本消息的内容进行相应的响应;
3、模拟粉丝发送文本消息给专用测试公众号,第三方平台方需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝。
代码如下:
[php] view plain copy
 
  1. $xmlTpl = "<xml>  
  2.             <ToUserName><![CDATA[%s]]></ToUserName>  
  3.             <FromUserName><![CDATA[%s]]></FromUserName>  
  4.             <CreateTime>%s</CreateTime>  
  5.             <MsgType><![CDATA[text]]></MsgType>  
  6.             <Content><![CDATA[%s]]></Content>  
  7.             </xml>";  
  8. $keyword = isset ( $param ['Content'] ) ? trim ( $param ['Content'] ) : '';  
  9. if(isset($param ['Event']) && $param ['ToUserName'] == 'gh_3c884a361561'){ // 案例1  
  10.     $contentStr = $param ['Event'] . 'from_callback';  
  11. }elseif ($keyword == "TESTCOMPONENT_MSG_TYPE_TEXT") { // 案例2  
  12.     $contentStr = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";  
  13. elseif (strpos ( $keyword, "QUERY_AUTH_CODE:" ) !== false) { // 案例3  
  14.     $ticket = str_replace ( "QUERY_AUTH_CODE:", "", $keyword );  
  15.     $contentStr = $ticket . "_from_api";  
  16.     $tokenInfo = WechatOpenApiLogic::getAuthorizerAccessTokenByAuthCode ( $ticket );  
  17.     $param ['authorizerAccessToken'] = $tokenInfo ['authorization_info'] ['authorizer_access_token'];  
  18.     self::sendServiceMsg ( $param ['FromUserName'], $param ['ToUserName'], 1, $contentStr ); // 客服消息接口  
  19.     return 1;  
  20. }  
  21. $result = '';  
  22. if (! empty ( $contentStr )) {  
  23.     $result = sprintf ( $xmlTpl, $param ['FromUserName'], $param ['ToUserName'], time (), $contentStr );  
  24.     if (isset ( $_GET ['encrypt_type'] ) && $_GET ['encrypt_type'] == 'aes') { // 密文传输  
  25.         $msgCryptObj = new WXBizMsgCrypt ( OPEN_MSG_VERIFY_TOKEN, OPEN_ENCRYPT_KEY, OPEN_APPID );  
  26.         $encryptMsg = '';  
  27.         $msgCryptObj->encryptMsg ( $result, $_GET ['timestamp'], $_GET ['nonce'], $encryptMsg );  
  28.         $result = $encryptMsg;  
  29.     }  
  30. }  
全网发布接入检测:

写的不是很详细,有疑问留言。。。

posted on 2017-12-17 18:27  浙江百牛信息技术  阅读(468)  评论(0编辑  收藏  举报

导航