I'm trying to parse JSON and extract a specific key, called faceId from it using PHP. But, when I try to parse it, I face the following error.
<b>Notice</b>: Undefined index: faceId in <b>/storage/ssd1/900/12273900/public_html/upload_image.php</b> on line <b>30</b><br />
I/flutter (18628): <br />
here's my JSON
[
{
"faceId": "a1e0ee95-3365-40b0-91f2-e0a05bdeadcc",
"faceRectangle": {
"top": 158,
"left": 298,
"width": 226,
"height": 226
},
"faceAttributes": {
"age": 19.0
}
}]
This is how I'm trying to parse it but it always throws the error.
$data = array('image' => $imageUrl);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result1 = file_get_contents($url, true, $context);
$jsonFaceID = json_decode($result1,true);
$finalFaceID= $jsonFaceID['faceId'];//the error appears here.
I've tried it another way too but still the same error.
$jsonFaceID = json_decode($result1,false);//it returns a JSON object
$finalFaceID= $jsonFaceID->faceId;
I'm unable to get the logic behind the error. Any help would be really appreciated. Thanks a lot
I am getting the result through a POST request from MS Face API for detect_face.
That json is an object in an array
Array
(
[0] => stdClass Object
(
[faceId] => a1e0ee95-3365-40b0-91f2-e0a05bdeadcc
[faceRectangle] => stdClass Object
(
[top] => 158
[left] => 298
[width] => 226
[height] => 226
)
[faceAttributes] => stdClass Object
(
[age] => 19
)
)
)
So the code should be
$finalFaceID= $jsonFaceID[0]->faceId;