Добрый день. В yii я новичок, пару дней. Нужно поймать json, разобрать его и занести в базу данных. Сначала была ошибка "unable to verify your data submission", когда я пытался отправлять json для проверки через curl. Поискав информацию, изменил beforeAction() в vendor/yiisoft/yii2/web/Contoller.php
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public function beforeAction($action)
{
if($action->id=="add")
{
$this->enableCsrfValidation=false;
}
if(!parent::beforeAction($action)) {
return false;
}
/*
if (parent::beforeAction($action)) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
}
return true;
}
*/
return true;
} |
|
Ошибка пропала, но все равно не работает.
UserdataController.php
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
| <?php
namespace app\controllers;
use app\models\Userdata;
use yii\web\Controller;
use yii\data\Pagination;
class UserdataController extends Controller
{
public function actionIndex()
{
$query = Userdata::find();
$pagination = new Pagination([
'defaultPageSize' => 15,
'totalCount' => $query->count(),
]);
$userdatas= $query->orderBy('date_start')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('index',[
'userdatas' => $userdatas,
'pagination' => $pagination,
]);
}
public function actionAdd()
{
$res = json_decode(file_get_contents("php://input"));
if(!empty($res))
{
$current_date=date("Y-m-d H:i:s");
$model = new Userdata();
if($res->event_name == "chat_accepted")
{
$model->date_start = $current_date;
$model->id = $res->chat_id;
$model->name = $res->visitor->name;
$model->email = $res->visitor->email;
$model->save();
}
if($res->event_name == "chat_finished")
{
$model=Post::model()->findByPk($res->chat_id);
$model->date_end = $current_date;
$model->chat = $res->chat->messages;
$model->save();
}
}
}
} |
|
Отправляю json так
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <?php
$r_data = array('event_name' => 'chat_accepted', 'chat_id' => 3);
$data_enc = json_encode($r_data);
$ch = curl_init('http://127.0.0.1/basic/web/index.php?r=userdata/add');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_enc);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Получаем данные
$response = curl_exec($ch);
echo $response; |
|
Добавлено через 2 часа 27 минут
Все правильно, в общем. Но из-за не тех инструкций из интернета кое-что не работало. Теперь всё ок.
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| public function actionAdd()
{
$res = json_decode(file_get_contents("php://input"));
if(!empty($res))
{
$current_date=date("Y-m-d H:i:s");
$model = new Userdata();
$vbaze = Userdata::find()->where(['id'=>$res->chat_id])->one();
if($res->event_name === "chat_accepted" && !$vbaze)
{
echo "accepted";
$model->date_start = $current_date;
$model->id = $res->chat_id;
$model->name = $res->visitor->name;
$model->email = $res->visitor->email;
$model->save();
}
if($res->event_name === "chat_finished" && $vbaze)
{
echo "finished";
$vbaze->date_end = $current_date;
$vbaze->chat = $res->chat->messages;
$vbaze->save();
}
}
echo "good";
} |
|