首页 / 话题 / 支付乐 / 文章详情

免公众号接口对接文档

一直在的 2022-04-01 | 阅读(4662) | 评论(4

常见问题一:域名需要备案吗?

答:不需要。

常见问题二:多个网站可以对接使用吗?

答:可以。

开始对接:

官方对接文档:

$appid = '';//appid
$state = '';//自定义参数,一般用于上级id,跟着code参数一起返回
$myurl="http://www.xxx.com/open";//接受code参数的链接
$wxurl='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$myurl.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
header('Location:'.$wxurl);

修改之后的样子:

$appid = 'wxc22deb8a0a93964b';//appid
$state = '';//自定义参数,一般用于上级id,跟着code参数一起返回
$myurl="http://www.xxx.com/open";//接受code参数的链接
$wxurl='http://api.lswww.top:88/callback.php?appid='.$appid.'&redirect_uri='.$myurl.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
header('Location:'.$wxurl);

获取用户信息:

public function open(){
	$appid='wxc22deb8a0a93964b';//支付乐免公众号接口的appid
	$appsecret='ffb4189cd83c0a3fe82b205402a6c0e3';//支付乐免公众号接口的appsecret
	$code   = $_GET['code'];//接收到的code
	$state  = $_GET['state'];//接收到的自定义参数
	if ($code) {
	  //初始化
	  $ch = curl_init();
	  //设置选项,包括URL
	  curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code");
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	  curl_setopt($ch, CURLOPT_HEADER, 0);
	  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	  //执行并获取HTML文档内容
	  $output = curl_exec($ch);
	  //释放curl句柄
	  curl_close($ch);
	}
	
	$oauth = json_decode($output,true);
	
	if(empty($oauth["access_token"])){
	  die('无法获取access_token,可能需要过白ip');
	}
	$access_token = $oauth["access_token"];
	
	$openid = $oauth['openid'];
	
	//第二步:根据全局access_token和openid查询用户信息
	$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
	$userinfo = $this->http_curl($get_user_info_url);
	
	//如果获取到了用户信息
	if($userinfo){
		echo $userinfo['openid'];//用户openid
		echo $userinfo['nickname'];//用户昵称
		echo $userinfo['headimgurl'];//用户头像
	}else{
		die('没有获取到用户信息');
	}
	
	function http_curl($url){
	    //用curl传参
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	
	    //关闭ssl验证
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	
	
	    curl_setopt($ch,CURLOPT_HEADER, 0);
	    $output = curl_exec($ch);
	    curl_close($ch);
	    return json_decode($output, true);
	}
}

如果你看不懂代码可能看着是一脸懵逼,但是不要慌,你只需要在原来官方对接文档的基础上替换三样东西:

1、appid

2、appsecret

3、接口地址

这样一说好像很简单,换三样东西就好了,那有的人又要问了,官网对接文档在哪里,我找不到啊,这怎么换?

1、全局搜索你自己的源码:https://open.weixin.qq.com/connect/oauth2/authorize 把这个地址换成支付乐的公众号接口地址:http://api.lswww.top:88/callback.php

2、凭着自己的经验和思考去找,因为每一套源码对接文档的位置都是不一样的,

那我的源码之前没有对接过微信登录这功能要怎么对接?

这个的话就要看你的技术了,如果你技术到位,那么完全可以参考此文档给你自己的网站对接一个微信登录功能

评论(4)
请登录
请登录后发表评论
  • 黑蛋 回复于:2022-09-21

    真的很负责,谢谢博主

  • 久伴科技 回复于:2022-10-02

    拿走了,感谢作者

  • Demon. 回复于:2022-10-02

    对小白真的很有帮助,写得很全面。

  • kim-( 回复于:2022-10-28

    终于找到这篇文章了,感谢作者的分享!