您的位置:首页 > 移动开发 > 微信开发

微信开发[测试账号]

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";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: