PHP对接微信第三方授权

提示:记录PHP对接微信第三方授权开发流程、相关注意事项以及在开发中遇到的问题和使用的相关函数

一、准备工作

创建平台型服务商

创建第三方平台应用填写相关配置,授权事件接收URL就是接收 ticket 链接地址。在这里插入图片描述

二、对接开发

1、接收微信推送数据,解密并缓存ticket 启动文档
  • 服务端请求开启 ticket 推送;
  • 解密方法使用官方DEMO即可,需要注意的是 PHP版本过高会提示 Call to undefined function: mcrypt_module_open,降低PHP版本即可,我使用的是7.1
  • 下面是涉及的比较常用的函数。
//获取xml数据
$data= file_get_contents("php://input")

//xml转换array 
$data = json_decode(json_encode(simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA)), true);
2、获取token
  • 根据appi、appsecret 和 ticket 获取token 并缓存
   /**
	 * 获取token  第三方平台接口的调用凭据 (代码仅供参考,具体请根据自己业务逻辑修改)
	 * Date 2021/6/23 下午 02:13
	 * @Author Li
	 */
	protected function getToken()
	{
		global $_W;
		$token = cache('wechat_manage_token');
		if(!$token){
			$url = $this->url_prefix . 'api_component_token';
			$post_data = [
				'component_appid' => $this->config['appId'],
				'component_appsecret' => $this->config['appsecret'],
				'component_verify_ticket' => cache('wechat_manage_ticket'),
			];
			$result = $this->http_post_data_json($url,$post_data);
			$result = json_decode($result,true);
			if(isset($result['errcode'])){
				return false;
			}
			$token = $result['component_access_token'];
			cache('wechat_manage_token', $token, time()+$result['expires_in']);
		}
		return $token;
	}
	//发送post请求
	function http_post_data_json($url, $data, $timeout = 5)
	{
		$jsonStr = json_encode($data);
		$ch      = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Content-Type: application/json',
				// 'Content-Length: ' . strlen($jsonStr)
			)
		);
		$response = curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);

		return $response;
	}
3、获取预授权码生成授权链接

生成预授权码,拼接授权链接。
PC链接参数:auth_type (选填) 要授权的帐号类型:1 则商户点击链接后,手机端仅展示公众号、2 表示仅展示小程序,3 表示公众号和小程序都展示。如果为未指定,则默认小程序和公众号都展示。第三方平台开发者可以使用本字段来控制授权的帐号类型。

PC链接:https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=3。

移动端:https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&no_scan=1&component_appid=xxxx&pre_auth_code=xxxxx&redirect_uri=xxxx&auth_type=xxx&biz_appid=xxxx#wechat_redirect

4、授权码获取授权信息
	/**
	 * 授权码兑换授权信息
	 * Date 2021/6/25 下午 04:44
	 * @Author Li
	 */
	public function queryAuth($auth_code = '', $supply_id = 0)
	{
		$token = $this->getToken();
		$url   = $this->url_prefix . 'api_query_auth?component_access_token='.$token;
		if(!$token) return ['code' => 0, 'msg' => '第三方平台调用凭证获取失败'];
		$post_data = [
			'component_appid'        => $this->config['appId'],
			'authorization_code'     => $auth_code,
		];
		$result    = $this->http_post_data_json($url, $post_data);
		$result    = json_decode($result, true);
		if(isset($result['errcode'])) {
			return ['code' => 0, 'msg' => $result['errmsg']];
		}
		$update = [
			'authorizer_status'        => 1,
			'authorizer_appid'         => $result['authorization_info']['authorizer_appid'],
			'authorizer_refresh_token' => $result['authorization_info']['authorizer_refresh_token'],
		];
		//保存授权appi 和 refresh_token 到数据库  以便更新小程序接口调用令牌时使用
		//缓存授权token  $result['authorization_info']['authorizer_access_token']
		return $result;
	}

总结

官方文档 写的还是很详细的,按照官方文档开发即可实现小程序授权第三方开发。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值