简体   繁体   中英

Receive data in Json in PHP

I am posting data to my script file to receive data. In my script file, File.php , I am not able to get the object patient in the dumped results. When i do var_dump($get_patient_info->patient);, it throws an error saying Object {patient} not found.

Could i be mapping the data wrongly?

PS: Beginner in Laravel

SendingData Controller

$hospitalData = [];
$hospitalData[] = [           
            'patient' => 'Mohammed Shammar',
            'number' => '34',
               ],

        $url = "https://example.com/file.php";
        $client = new Client();
        $request = $client->post($url, [
            'multipart' => [
                [
                    'name' => 'patient_info',
                    'contents' => json_encode($hospitalData),
                ],
            ],
        ]);
        $response = $request->getBody();
        return $response;

File.php

$get_patient_info = $_POST['patient_info'];

          var_dump($get_patient_info);

Results

string(189) "[{"patient":"Mohammed Shammar","number":"34"}]"

You can json_decode and fetch the data as follows,

$temp = json_decode($get_patient_info); 
echo $get_patient_info[0]->patient;

json_decode — Decodes a JSON string

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM