微信开发[测试账号]

一、微信开发: Oauth 认证

所需:服务器一台
微信 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
配置网页授权ip地址或者域名
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";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值