PHP -- YII2框架微信公众号中实现表单提交的功能
2018-02-19 19:35:26
•
阅读
打赏
下面通过本文给大家分享PHP -- YII2框架微信公众号中实现表单提交的功能,感兴趣的朋友一起看看吧
每个关注的用户在同一个公众号当中的openid是唯一性的。所以在打开这个表单页面的时候,我们需要获取这个openid,然后存到数据库中,方便下次提交验证。
下面是php代码,使用的是YII2框架。
Controller
//回调函数
public function actionCallback($code, $state)
{
$model = new tp_tstz_proposal();
$model1 = new tp_tstz_staff();
// 微信开放平台网站应用的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) {
// 向微信请求授权时出错,打印错误码
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]);
}
}
view层
重定向页面
header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8ba95fc51672e844&redirect_uri=http%3a%2f%2fjifen.aa.cn%2fts%2fweb%2findex.php%3fr%3dproposal%2fcallback&response_type=code&scope=snsapi_base&state=123asd#wechat_redirect');
返回的路径就是进入controller的路径。
在表单页面,做了一个简单的认证
if (!isset($key)) {
header('Location:http://jifen.aa.cn/ts/web/index.php?r=say/login');
}
以上所述是小编给大家介绍的PHP -- YII2框架微信公众号中实现表单提交的功能,希望对大家有所帮助。在此也非常感谢大家对本站的支持
相关推荐
深度学习 -- 损失函数
深度残差网络(Deep Residual Networks (ResNets))
深度学习 -- 激活函数
神经网络训练 -- 调整学习速率
生成对抗网络(GAN)改进与发展
生成对抗网络(GAN)优点与缺点
生成对抗网络(GAN)的训练
生成对抗网络(GAN)基本原理
生成模型与判别模型