第一步:用户同意授权,获取code
在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认带有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:
https://open.weixin.qq.com/connect/oauth3/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
所以
先用调试接口生成自定义菜单,菜单json如下:
{
"button":[
{
"name":"风信科技",
"sub_button":[
{
"type":"click",
"name":"关于风信",
"key":"FS_V1_1001"
},
{
"type":"click",
"name":"我们的服务",
"key":"FS_V1_1002"
},
{
"type":"click",
"name":"成功案例",
"key":"FS_V1_1003"
},
{
"type":"click",
"name":"解决方案",
"key":"FS_V1_1004"
},
{
"type":"click",
"name":"OA通讯录",
"key":"FS_V1_1005"
}]
},
{
"name":"风信产品",
"sub_button":[
{
"type":"click",
"name":"平台产品",
"key":"FS_V2_1001"
},
{
"type":"click",
"name":"领域产品",
"key":"FS_V2_1002"
},
{
"type":"click",
"name":"行业产品",
"key":"FS_V2_1003"
},
{
"type":"view",
"name":"微办公",
"url":"https://open.weixin.qq.com/connect/oauth3/authorize?appid=$APPID&redirect_uri=$URL&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
}]
},
{
"name":"风信娱乐",
"sub_button":[
{
"type":"click",
"name":"抽奖活动",
"key":"FS_V3_1001"
},
{
"type":"click",
"name":"查询天气",
"key":"FS_V3_1002"
},
{
"type":"view",
"name":"更多活动",
"url":"http://www.funsing.com/"
}]
}]
}
第二步,获取openId,
public function index() { $code = I('get.code'); $weid = $this->getOpenId($code); if ($weid == "") { $this->redirect('/Home/Index/login', array(), 0, "waiting..."); } else { $result = json_decode($this->client->IsAccount(array("weId"=>$weid))->IsAccountResult); $info = $result->ErrMsg; $this->gotoPage($info, $weid); } }
//通过code取得openid public function getOpenId($code) { $urlpre = "https://api.weixin.qq.com/sns/oauth3/access_token?appid=".$this->appId."&secret=".$this->appsecret."&grant_type=authorization_code&code="; $url = $urlpre.$code; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); //$access_token = $jsoninfo["access_token"]; $openid = $jsoninfo["openid"]; return $openid; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。