如何在Laravel中获取和验证application/json数据? [英] How to get and validate application/json data in Laravel?

查看:599
本文介绍了如何在Laravel中获取和验证application/json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以application/json内容类型将数据从客户端发送到服务器.

I send data from client to server in application/json content type.

然后我尝试像下面这样在服务器端获取此信息:

Then I try to take this information in server side like as:

public function register(Request $request)
{

        $data = $request->json()->all();
        var_dump($data); die();
}

它返回我为空array()

我也尝试使用以下方法验证传入的POST:

Also I tried to validate incoming POST using this:

$validator = Validator::make($request->json()->all(), []);

如何在Laravel中获取和验证应用程序/json数据?

How to get and validate application/json data in Laravel?

我得到如下的POST数据:

I get POST data like as:

dd($_POST);

array:1 [▼
  "application/json" => "{"id":6,"unique_code":null,"name":"О","secondname":"П","lastname":"Валерьевич","datebirth":"14/10/1991 00:00:00","taxcode":"4545","gender":"1","created_at":null,"file":"C:\\db\\tests\\22-07-2017\\MMM1.TXT","orders":{"profession":"Директор","pacient_id":null,"payment":"1","kind_work":"1,2","factory_name":"FALKO","factory_edrpou":"2020","factory_departament":"IT","status_pass":"1","office_address":"Kiev","unique_code":"0","enterprise_id":"12","status":null},"http_code":null}"
]

推荐答案

我有一个将json发布到的api.我有一个api端点,我在其中发布了这个json

I have an api I post json to. I have an api end point where I post this json

{ "email":"youremail@triumworks.com", "phone": "phone", "name": "name", "password": "password" } 处理请求的相应控制器看起来像

{ "email":"youremail@triumworks.com", "phone": "phone", "name": "name", "password": "password" } The corresponding controller that handles the request looks like

public function create_account(Request $request){
    $data = json_decode(file_get_contents('php://input'));
    $response = new Responseobject;     
    $array_data = (array)$data;

    $validator = Validator::make($array_data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6',
        'phone' => 'required|string|min:12|max:12|unique:users',
    ]);

    if($validator->fails()){
        $response->status = $response::status_failed;
        $response->code = $response::code_failed;
        foreach ($validator->errors()->getMessages() as $item) {
            array_push($response->messages, $item);
        }   
    }
    else{
        $api_token = str_random(60);
        $user = new User();
        $user->api_token = $api_token;
        $user->name = $data->name;
        $user->email = $data->email;
        $user->phone = $data->phone;
        $user->password = bcrypt($data->password);

        if($user->save()){
            $response->status = $response::status_ok;
            $response->code = $response::code_ok;
            $response->result = $user;
        }           
    }
    return Response::json(
            $response
        );
}

这和上面的一样.

public function create_account(Request $request){
        $response = new Responseobject();

        $validator = Validator::make($request->json()->all(), [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6',
            'phone' => 'required|string|min:12|max:12|unique:users',
        ]);

        if($validator->fails()){
            $response->status = $response::status_failed;
            $response->code = $response::code_failed;
            foreach ($validator->errors()->getMessages() as $item) {
                array_push($response->messages, $item);
            }   
        }
        else{
            $api_token = str_random(60);
            $user = new User();
            $user->api_token = $api_token;
            $user->name = $data->name;
            $user->email = $data->email;
            $user->phone = $data->phone;
            $user->password = bcrypt($data->password);

            if($user->save()){
                $response->status = $response::status_ok;
                $response->code = $response::code_ok;
                $response->result = $user;
            }           
        }
        return Response::json(
                $response
            );
    }

这篇关于如何在Laravel中获取和验证application/json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆