微信开发[测试账号]
2016-06-24 16:07
459 查看
一、微信开发: Oauth 认证
所需:服务器一台微信 Oauth 授权过程与服务器认证
其实在正常开发的时候,可以省略一步,就是在菜单栏哪里配置 微信OAuth 链接就好,有一点需要注意的是作为参数的url 必须要 URLEncode 一下。
测试号开发:
1、开通微信公众号、进入公众号平台
2、菜单栏–>开发者工具–>公众平台测试账号,点击进入;
3、要微信扫码进入
4、在这个界面我们可以获取到
appID appsecret
修改接口信息配置: url 和 Token(字符串,在验证接口会用到)
接入指南
1)url必须为可通过get方法访问的,其中要接收:signature、timestamp、nonce、echostr 参数
/** * 检验服务器有效性 * @return mixed */ public function valid(){ $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $echostr = $_GET["echostr"]; $token = $this->token; //为配置的 Token $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return $echostr; }else{ return null; } }
配置完成后点击提交,会提示配置结果。
2)配置OAuth2.0 网页授权,注意不要不能加协议,是www.baidu.com,而不是https:www.baidu.com
5、配置完成后,接下来就是核心代码(这代码之供参考,了解流程进行)
/** * 获取 openId session 中有保存就直接返回session中的openId * 没有就重定向微信 oauth 验证页面 */ public function action_getOpenId2(){ $open_id = session(“user_open_id”); if ($open_id){ return $open_id; }else{ // 设置微信回调 地址 $return_url = \Fuel\Core\Uri::create("admin/weixin/getAccessionToken2"); $url = self::authGetCode2($return_url); \Fuel\Core\Response::redirect($url); } } /** * 从微信回调时带过来的参数中,或去 code 参数用于请求 openId */ public function action_getAccessionToken2(){ $code = $_GET['code']; $openId = self::getOpenId2($code); // 拿到 openId 将其保存在 session 中 session("user_open_id",$openId); return $openId; } /** * 组装成完整的微信 OAuth 请求地址 */ static function authGetCode2($return_url,$state="STATE"){ $redirect_uri = urlencode($return_url) ; $appId = self::APPID; $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appId&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=$state#wechat_redirect"; return $url; } /** * curl 请求微信接口 获取 openId */ static function getOpenId2($code){ $appId = self::APPID; $secret = self::APPSECRET; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appId&secret=$secret&code=$code&grant_type=authorization_code"; $opts = array( CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL =>$url ); $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if($error) throw new Exception('请求发生错误:' . $error); $array = json_decode($data,true);; return $array["openid"]; }
二、接收用户信息
在 Oauth 中提到的 服务器 URL 提供一个 post 请求的控制器工作流程:
1、用户在公众号发送信息;
2、微信收到用户的信息,然后转发到我们设置好的url;
3、服务器接收到微信的信息,处理,返回 success 给微信。
public function valid() { $request_method = $_SERVER["REQUEST_METHOD"]; if( $request_method == 'GET'){ $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $echostr = $_GET["echostr"]; $token = $this->token; //为配置的 Token $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return $echostr; }else{ return null; } }else if( $request_method == 'POST'){ // 处理微信发送过来的请求,然后是用微信提供的 sdk 做想要的操作 }else{ return "ok"; } }
相关文章推荐
- 微信支付流程 和 支付宝支付
- 微信上传图片到自己的服务器
- php 微信扫码支付几个坑
- 微信公众号开发者中心配置 Token验证失败 终极解决方案
- MOB 微信分享闪退 errCode -6
- 微信订阅号和公众号的区别
- iOS软件开发实现类似微信上传图片选择
- 微信开发-回调模式
- Android 录音实现方法、仿微信语音、麦克风录音、发送语音、解决5.0以上BUG
- 微信 Jssdk 修改到无错的版本
- 微信公众平台开发之获得ACCESSTOKEN .Net代码解析
- 微信公众平台开发之认证"成为开发者".Net代码解析
- java微信开发之上传下载多媒体文件
- 微信公众号判断用户是否已关注php代码解析
- php微信开发之上传临时素材
- PHP微信开发之模板消息回复
- PHP微信开发之根据用户回复关键词\位置返回附近信息
- iOS开发集成微信支付
- 小程序分析(fork()和vfork())
- PHP微信开发之模板消息回复