Answer the question
In order to leave comments, you need to log in
Why does Yii2 REAST api accept json when testing via postman, but only sees an empty array when I send REST api with json from php file?
When I test my api through postman, everything works, the data is added to the table. But when I try to test from a php file, I get an error {"status":false,"date":{"email":["Email cannot be blank."],"pass":["Pass cannot be blank."] }}, i.e. fields are passed empty/not passed at all.
Before sending, I make sure that json is formed correctly, but return \Yii::$app->request->post(); returns [] - an empty array?
I can't figure out what I'm doing wrong when sending email and pass data. Has anyone come across this before and found a solution?
PHP file code from which I am sending data
<?php
$fields['email'] = 'Aria';
$fields['pass'] = '000';
$json_str = json_encode($fields,JSON_UNESCAPED_UNICODE);
echo $json_str."\n"; //{"email":"Aria","pass":"000"}
$url = 'http://192.168.88.242:8080/api/users/create-users';
$response = file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: Application/JSON',
'content' => $json_str
)
)));
Echo $response."\n";
$response = json_decode($response, true);
.......
die('END');
?>
public function actionCreateUsers()
{
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$users = new Users();
$users->scenario=Users::SCENARIO_CREATE;
$users->attributes= \Yii::$app->request->post();
return \Yii::$app->request->post(); //Чтобы увидеть что получает в качестве данных, через postman это json, а от php - файла [] =(
if($users->validate()){
$users->save();
return array('status' => true,'date'=>'User created successfuly'); //выдает в тестах через postman
} else {
return array('status'=> false,'date'=> $users->getErrors()); //выдает в тестах через php-файл
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question