《PHP教程:使用YII2框架實(shí)現(xiàn)微信公眾號中表單提交功能》要點(diǎn):
本文介紹了PHP教程:使用YII2框架實(shí)現(xiàn)微信公眾號中表單提交功能,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
PHP教程剛接觸微信,要做一個(gè)在手機(jī)上的表單提交功能.
PHP教程需求有這些:
PHP教程因?yàn)槊總€(gè)手機(jī)在同一個(gè)公眾號當(dāng)中的openid是唯一性的.所以在手機(jī)查看這個(gè)表單頁面的時(shí)候,就將這個(gè)openid存到數(shù)據(jù)庫中,方便下次提交可以驗(yàn)證.
PHP教程下面是我的代碼.使用的是YII2框架.
PHP教程Controller
PHP教程 //獲得回調(diào)函數(shù) public function actionCallback($code,$state){ $model = new tp_tstz_proposal(); $model1= new tp_tstz_staff(); // 微信開放平臺網(wǎng)站應(yīng)用的appid和秘鑰secret $appid = ''; $secret = ''; $curl = new curl\Curl(); //獲取access_token $wxresponse = $curl->get('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code'); $wxresult = json_decode($wxresponse); if(isset($wxresult->errcode) && $wxresult->errcode > 0){ //分享出去,重新認(rèn)證 return $this->render('login'); // 向微信請求授權(quán)時(shí)出錯(cuò),打印錯(cuò)誤碼 // echo json_encode($wxresult); // exit; } $openid=$wxresult->openid; $result=$model1::find()->where(['openid'=>$openid])->one(); //如果OPENID存在就去表單 if(count($result)>0){ $key=123456; return $this->render('view',['model'=>$model,'key'=>$key]); }else{ return $this->render('tel',['model'=>$model1,'openid'=> $openid]); } }`
PHP教程view層
PHP教程很簡單的重定向頁面
PHP教程 header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8ba95fc51672e844&redirect_uri=http%3a%2f%2fjifen.wendu.cn%2fts%2fweb%2findex.php%3fr%3dproposal%2fcallback&response_type=code&scope=snsapi_base&state=123asd#wechat_redirect');
PHP教程返回的路徑就是進(jìn)入controller的路徑.
PHP教程在表單頁面,我先做了一個(gè)簡單的認(rèn)證
PHP教程 if(!isset($key)){ header('Location:http://jifen.wendu.cn/ts/web/index.php?r=say/login'); }
PHP教程判斷是否是從分享的頁面來的,如果是從分享的頁面來就要重新驗(yàn)證,判斷是否在數(shù)據(jù)庫中有此手機(jī)的openid.沒有就進(jìn)行手機(jī)號碼的驗(yàn)證.
PHP教程大概就是這樣了,我第一個(gè)簡單的微信公眾號項(xiàng)目.
PHP教程總結(jié)
PHP教程以上所述是小編給大家介紹的使用YII2框架實(shí)現(xiàn)微信公眾號中表單提交功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的.在此也非常感謝大家對腳本之家網(wǎng)站的支持!
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/181.html