微信公众号第三方代授权Thinkphp版

  • q2_548262
    了解作者
  • 7.4KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 07:26
    上传日期
Thinkphp版的微信公众号第三方代授权
微信公众号第三方代授权.zip
  • index.php
    17.3KB
  • wxBizMsgCrypt.php
    13.3KB
内容介绍
<?php include_once "wxBizMsgCrypt.php"; class AuthorizationAction extends Action { public $component_access_token; public $weixin_account; public $accountM; public $userid; public $token; public $pubs; public function __construct() { parent::__construct(); $this->userid = 1; $this->token = $_SESSION['token']; $this->accountM = new Model('weixin_account'); $this->pubs = new Model('pubs'); $this->weixin_account = $this->accountM->where(array( 'id' => $this->userid ))->find(); } //发起授权页的体验URL public function login() { $get_pre_auth_code = $this->get_pre_auth_code(); echo "<a href='https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=" . $this- rel='nofollow' onclick='return false;'>weixin_account['appId'] . "&pre_auth_code=$get_pre_auth_code&redirect_uri=http://www.weiyes.com.cn/index.php/Authorization/test'><img src='https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_appwx_logo.png'></a>"; } //公众号消息与事件接收URL public function test() { $msg_signature = $_REQUEST['msg_signature']; if ($msg_signature) { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); file_put_contents('ccccsimplexml_load_string.txt',json_encode($postObj)); $toUsername = $postObj->ToUserName; if ($toUsername == 'gh_3c884a361561') { $this->encrypt($postStr); }else{ $this->encrypt($postStr); } } else { return; } } /* 解密 */ public function encrypt($encyptdata) { $encodingAesKey = $this->weixin_account['encodingAesKey']; $token = $this->weixin_account['token']; $appId = $this->weixin_account['appId']; $timeStamp = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']); $nonce = empty($_GET['nonce']) ? "" : trim($_GET['nonce']); $msg_sign = empty($_GET['msg_signature']) ? "" : trim($_GET['msg_signature']); $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId); $xml_tree = new DOMDocument(); $xml_tree->loadXML($encyptdata); $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) { $msgObj = json_decode(json_encode(simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA))); file_put_contents('cccchenzhiqmsgObj.txt',json_encode($msgObj)); // {"ToUserName":"gh_3c884a361561","FromUserName":"ozy4qt1eDxSxzCr0aNT0mXCWfrDE","CreateTime":"1446104321","MsgType":"event","Event":"LOCATION","Latitude":"111.000000","Longitude":"222.000000","Precision":"333.000000"} switch ($msgObj->MsgType) { case "event": $this->replyEventMessage($timeStamp, $nonce, $msg_sign, $msgObj->Event, $msgObj->ToUserName, $msgObj->FromUserName); break; case "text": $this->processTextMessage($timeStamp, $nonce, $msg_sign, $msgObj->Content, $msgObj->ToUserName, $msgObj->FromUserName); break; default: break; } } else { file_put_contents('cccxmlerr.txt', json_encode($errCode) . date('Y-m-d H:i:s', time()) . "/n", FILE_APPEND); } } public function processTextMessage($timeStamp, $nonce, $msg_sign, $Content, $toUserName, $fromUserName) { if ('TESTCOMPONENT_MSG_TYPE_TEXT' == $Content) { $text = $Content . '_callback'; file_put_contents('cccTESTCOMPONENT_MSG_TYPE_TEXT.txt', $Content); $this->replyTextMessage($timeStamp, $nonce, $msg_sign, $text, $toUserName, $fromUserName); } elseif (stristr($Content, "QUERY_AUTH_CODE")) { $textArray = explode(':', $Content); file_put_contents('cccQUERY_AUTH_CODE.txt', $Content); $this->replyApiTextMessage($timeStamp, $nonce, $msg_sign, $textArray[1], $toUserName, $fromUserName); }else{ $this->replyTextMessage($timeStamp, $nonce, $msg_sign, $Content, $toUserName, $fromUserName); } } public function replyApiTextMessage($timeStamp, $nonce, $msg_sign, $query_auth_code, $toUserName, $fromUserName) { $url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $this->get_Access_token(); $data = json_encode(array( 'component_appid' => $this->weixin_account['appId'], 'authorization_code' => $query_auth_code )); $getreplyApiTextMessage = json_decode($this->curl_get_post($url, $data)); file_put_contents('ccccgetreplyApiTextMessage.txt', json_encode($getreplyApiTextMessage)); $text = $query_auth_code . '_from_api'; $sfromUserName = $getreplyApiTextMessage->authorization_info->authorizer_appid; $authorizer_access_token = $getreplyApiTextMessage->authorization_info->authorizer_access_token; $authorization = $this->get_authorization($sfromUserName, 'customer_service', '1'); if ($authorization == 'ok') { file_put_contents('ccccget_authorization.txt', 'ok'); $this->processWechatTextMessage($text, $fromUserName, $authorizer_access_token); } } /* *推送客服回复信息 */ function processWechatTextMessage($text, $fromUserName, $authorizer_access_token) { $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $authorizer_access_token; $data = json_encode(array( 'touser' => $fromUserName, 'msgtype' => 'text', 'text' => array( 'content' => $text ) )); file_put_contents('ccccprocessWechatTextMessagedata.txt', $data); $getreplyApiTextMessage = json_decode($this->curl_get_post($url, $data)); file_put_contents('ccccprocessWechatTextMessage.txt', json_encode($getreplyApiTextMessage)); } /* 获取授权 location_report(地理位置上报选项) 0无上报 1进入会话时上报 2每5s上报 voice_recognize(语音识别开关选项)0关 1开 customer_service(客服开关选项)0关 1开 */ function get_authorization($authorizer_appid, $option_name, $option_value = '1') { $arraydata = array( 'location_report', 'voice_recognize', 'customer_service' ); if (in_array($option_name, $arraydata)) { $url = 'https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option?component_access_token=' . $this->get_Access_token(); $data = json_encode(array( 'component_appid' => $this->weixin_account['appId'], 'authorizer_appid' => $authorizer_appid, 'option_name' => $option_name, 'option_value' => $option_value )); $query_authorization = json_decode($this->curl_get_post
评论
    相关推荐
    • php微信登录
      我在第一接触微信登录时,试了许多方法,也在网上搜索许多资料,但是对于微信登录,一直是云里来雾里去的整不明白,经历许多坑,也花费许多精力,为了让菜鸟们少走弯路,节约时间,我录制这个课程,我的原则就是用最...
    • 微信开发 PHP
      微信开发 PHP
    • 微信PHP开发框架LaneWeChat 1.5.3.zip
      LaneWeChat是一款基于PHP微信开发框架,高效开发微信公众号应用,简洁而又轻量。LaneWeChat 1.5.3 更新日志:2015-10-211、文件上传根据PHP版本增加了CURLFile()类。自PHP5.5以后,废弃了“@文件名”的...
    • php 微信开发
      微信开发代码实例 超全 超清晰,轻松带你走进微信大门
    • kppwapi微信php开发源代码
      不包含微信api开发,就是一个开源的更新包和官网一致。 开源技术
    • 微信PHP代码
      微信PHP代码微信
    • 微信PHP代码示例
      微信公众平台 开发 PHP代码示例
    • 微信开发php
      微信开发 php代码 入门级,提供一个简单的php封装包,只限回复文本信息
    • 微信群发PHP
      微信群发代码,模拟登录微信公共平台,实现信息发送
    • 微信支付PHP
      公众号支付,扫码支付,查询订单,退款,异步回调等,已封装好,可直接调用