您好,登錄后才能下訂單哦!
第一步:用戶同意授權(quán),獲取code
在確保微信公眾賬號(hào)擁有授權(quán)作用域(scope參數(shù))的權(quán)限的前提下(服務(wù)號(hào)獲得高級接口后,默認(rèn)帶有scope參數(shù)中的snsapi_base和snsapi_userinfo),引導(dǎo)關(guān)注者打開如下頁面:
https://open.weixin.qq.com/connect/oauth3/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
所以
先用調(diào)試接口生成自定義菜單,菜單json如下:
{
"button":[
{
"name":"風(fēng)信科技",
"sub_button":[
{
"type":"click",
"name":"關(guān)于風(fēng)信",
"key":"FS_V1_1001"
},
{
"type":"click",
"name":"我們的服務(wù)",
"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":"風(fēng)信產(chǎn)品",
"sub_button":[
{
"type":"click",
"name":"平臺(tái)產(chǎn)品",
"key":"FS_V2_1001"
},
{
"type":"click",
"name":"領(lǐng)域產(chǎn)品",
"key":"FS_V2_1002"
},
{
"type":"click",
"name":"行業(yè)產(chǎn)品",
"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":"風(fēng)信娛樂",
"sub_button":[
{
"type":"click",
"name":"抽獎(jiǎng)活動(dòng)",
"key":"FS_V3_1001"
},
{
"type":"click",
"name":"查詢天氣",
"key":"FS_V3_1002"
},
{
"type":"view",
"name":"更多活動(dòng)",
"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; }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。