I want to get data from API but instead keep getting 'Undefined Property' PHP error.
I am trying to get 'SESSION_ID' in Datas in Data. (JSON/POST)
{
"Data":
{
"Code":"00",
"Datas":
{
"COM_CODE":"80001",
"USER_ID":"USER_ID",
"SESSION_ID":"39313231367c256562253866253939256563253838253938:0HDD9DBtZt2e"
},
"Message":"",
"RedirectUrl":""
},
"Status":"200",
"Error":null
}
And my PHP code to retrieve SESSION_ID
is like this. First, sending $data
and get the SESSION_ID
.
function api_login(){
$data = array(
"COM_CODE" => "000000",
"USER_ID" => "USER"
);
$ch = curl_init( "https://apifakeurl.com/Login" );
$payload = json_encode($data);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result);
$session_id = $json->Data->Datas->SESSION_ID;
//Here is the line where I get the 'Undefined Property' error.
return $session_id;
}
As I am PHP beginner, I am not sure whether that one error line is the problem, or the whole structure is wrong. Please help!