yii中接收微信传过来的json数据

时间:2021-06-09 13:33:19
//控制器
<?php
namespace frontend\controllers;
use frontend\models\User;
use yii;
use yii\web\Controller;
class ContController extends Controller
{
public $enableCsrfValidation = false;
//用户注册
public function actionUser()
{
//接收json数据
$a=file_get_contents("php://input");
$aa=json_decode($a,true);
$last=User::add($aa);
if($last)
{
return "添加成功";
}
else
{
return "添加失败";
}
}
}
//模型层
<?php
namespace frontend\models;
use yii;
use yii\base\Model;
class User extends Model
{
//用户添加
public static function add(array &$data)
{
// return $data['name'];
$a=Yii::$app->db->createCommand()->insert('user', [
'name' => $data['name'],
'age' => $data['age'],
'se' => $data['se'],
'email' => $data['email'],
'tell' => $data['tell'],
'pwd' => $data['pwd'],
])->execute();
if($a)
{
return true;
}else{
return false;
}
}
}