1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 微信公众号授权openid(php实现)

微信公众号授权openid(php实现)

时间:2022-05-06 21:51:12

相关推荐

微信公众号授权openid(php实现)

第一步:

点击https://developers./doc/offiaccount/Basic_Information/Requesting_an_API_Test_Account.html

生成appIDappsecret

此时准备一个外网可访问的域名,需将域名配置在公众号内两个地方,JS接口安全域名和网页授权获取用户基本信息,不然授权不通过

用你的微信关注此测试公众号。

第二步:

写一个静态页面,该静态页面外网可访问。但是必须是用你关注此测试公众号的微信测试。

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml" lang="en_US" xml:lang="en_US"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><head><title>登陆</title> </head><script type="text/javascript">//var redirect_uri = "<?php echo $redirect_uri;?>";window.onload = function(){window.location.href ='https://open./connect/oauth2/authorize?appid=wx8052309468bad295&redirect_uri=https%3A%2F%%2Findex%2Findex%2Fweixin&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';};</script></html>

appid和回调地址redirect_uri(别忘了urlencode处理一下)填写你自己的。

第三步:

写一个xx.php,也就是你什么的那个回调地址里面的内容

//第一步:用户同意授权,获取code$code=$_REQUEST["code"];echo "<h1>您的code是:<span style='text-decoration:none;color:red;'>".$code."</span><h1><br>";//第二步:通过code换取网页授权access_token$urlwx="https://api./sns/oauth2/access_token?appid=wx8052309468bad295" ."&secret=1b36a7b311159d03aee68c55eca4fa81&code=".$code."&grant_type=authorization_code"; $jsonwx=json_decode(file_get_contents($urlwx),true);//echo "<pre>";// print_r($jsonwx);exit;//echo "</pre>";$access_token=$jsonwx["access_token"];$expires_in=$jsonwx["expires_in"];$refresh_token=$jsonwx["refresh_token"];$openid=$jsonwx["openid"];$scope=$jsonwx["scope"];echo "<h1>您的access_token是:<span style='text-decoration:none;color:red;'>".$access_token."</span><h1><br>";//第四步:拉取用户信息(需scope为 snsapi_userinfo)$urlwx="https://api./sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $userinfo=json_decode(file_get_contents($urlwx),true);echo "<h1>您的openid是:<span style='text-decoration:none;color:red;'>".$userinfo["openid"]."</span><h1><br>";//echo "<h1>您的unionid是:<span style='text-decoration:none;color:red;'>".$userinfo["unionid"]."</span><h1><br>";echo "<h1>您的姓名是:<span style='text-decoration:none;color:red;'>".$userinfo["nickname"]."</span><h1><br>";echo "<h1>您所在城市是:<span style='text-decoration:none;color:red;'>".$userinfo["country"].$userinfo["province"].$userinfo["city"]."</span><h1><br>";

第四步:

把你可外网访问html页面的路径复制到你的手机号随意发一个信息出去,然后点击访问。

参考网址/wxs55555/article/details/72652058

这几个小伙伴的文章也挺好的。

微信公众号开发之创建菜单栏代码示例(php)

/fps2tao/p/8661322.html

/fps2tao/p/8661322.html

微信自定义菜单+获取网页授权+获取用户信息

/weixin_38361347/article/details/88071066

php实现微信公众号创建自定义菜单功能的实例代码

/article/162800.htm

/xiaogou/p/7090379.html

/xiaogou/p/7090379.html

php微信公众号开发,入门篇(实现了关注公众号发送欢迎信息,发关键词自回复)

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