首页 > PHP教程 > php开发知识文章

PHP -- YII2框架微信公众号中实现表单提交的功能

下面通过本文给大家分享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框架微信公众号中实现表单提交的功能,希望对大家有所帮助。在此也非常感谢大家对本站的支持

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。