1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 微信第三方平台php api接口开发 微信公众号第三方开发平台(一)

微信第三方平台php api接口开发 微信公众号第三方开发平台(一)

时间:2019-07-28 13:18:31

相关推荐

微信第三方平台php api接口开发 微信公众号第三方开发平台(一)

最近在做一个公众号的工具,想使用公众号的权限,第一步就是授权,官方的文档说的不是很清楚,对于一个刚接触的开发者看起来是有点吃力的。

一、创建微信第三方应用

说下两个比较重要的域名设置

授权事件接收URL

微信服务器会向这个地址推送一些信息包括(ComponentVerifyTicket、用户取消应用授权...)

公众号消息与事件接收URL

用户关注公众号或发送信息对应的信息会推送到这个地址。

其他几个连接填你对应的域名就行了。

二、接收component_verify_ticket

微信调用任何接口都需要component_verify_ticket,这一步也是折腾我最久的,主要推送过来的信息是加密的我们需要将信息解密,解密出component_verify_ticket将该ticket保存后面需要用到

消息加解密接入指引

这个页面上可以下载对应语言解密的demo,我选择的是php,貌似有点问题,自己改了下可以用了。

分享下php修改后解密的代码

public function parseMess()

{

$data = $_REQUEST;

$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];

$encryptMsg = $postStr;

$xml_tree = new DOMDocument();

$xml_tree->loadXML($encryptMsg);

$array_e = $xml_tree->getElementsByTagName('Encrypt');

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

$msg_sign = $data['msg_signature'];

$format = "";

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

$msg = '';

$this->load->library('openweixin/wxBizMsgCrypt'); //改文件在php的demo包里面,加载过来就行

$pc = new WXBizMsgCrypt($this->token, $this->key, $this->appid);

$timeStamp = $data['timestamp'];

$nonce = $data['nonce'];

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

if ($errCode == 0) {

$postObj = simplexml_load_string($msg, "SimpleXMLElement", LIBXML_NOCDATA);

$data = (array)$postObj;

return $data;

}

接收微信服务推送的消息都需要解密,该方法都可以解密。

三、获取第三方平台component_access_token (调用接口需要的基础参数)

component_access_token这个参数也需要保存,后面也会重复用到。

这个参数的有效期是2个小时,所以定时每2小时执行下面的程序来更新component_access_token

public function getToken()

{

$ticket = $component_verify_ticket; //component_verify_ticket之前已经保存了,从数据库中获取

$post = array(

'component_appid' => $this->appid, //应用详情中的AppID

'component_appsecret' => $this->secret, //应用详情中的AppSecret

'component_verify_ticket' => $ticket,

);

$ret=send_post('https://api./cgi-bin/component/api_component_token', $post);

$result = json_decode($ret, True);

$component_access_token = $result['component_access_token'];

//保存component_access_token

echo 'success';

}

//发送post请求

function send_post($url, $post_data) {

$postdata=json_encode($post_data);

$options = array(

'http' => array(

'method' => 'POST',

'header' => 'Content-type:application/x-www-form-urlencoded',

'content' => $postdata,

'timeout' => 15 * 60 // 超时时间(单位:s)

)

);

$context = stream_context_create($options);

$result = file_get_contents($url, false, $context);

return $result;

}

获取预授权码pre_auth_code

有限时间是10分钟,所以定时执行每过10分钟来刷新一次pre_auth_code

//刷新pre_auth_code

public function refresh_pre()

{

$info = getData('system_config', array('type' => 'component_access_token'), array(), 1);

$component_access_token = $info ['value'];

$url = 'https://api./cgi-bin/component/api_create_preauthcode?component_access_token='.$component_access_token;

$post = array('component_appid' => $this->appid);

$ret=send_post($url, $post);

$result = json_decode($ret, True);

$pre_auth_code = $result['pre_auth_code'];

//保存pre_auth_code

echo 'success';

}

这是我的个人网站今日Tech 喜欢科技新闻的朋友可以收藏下。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。