There is an api $url that produces result in the following format:
{"status":true,"message":"Good morning","data":{"first_name":"Peter","last_name":"Rick","mobile":"09887","PIN":"1234"}}
I've tried file_get_contents as follows but not getting desired results.
$contents = file_get_contents($url);
$cont=json_decode($contents,true);
$cont = array($contents);
$message = $cont->message;
How do I get out the values of the variables status,message,first_name,last_name,mobile,PIN using file_get_contents
$contents = '{"status":true,"message":"Good morning","data":{"first_name":"Peter","last_name":"Rick","mobile":"09887","PIN":"1234"}}';
$cont=json_decode($contents);
$message = $cont->message;
echo $cont->data->first_name;
echo '<br />';
echo $cont->data->last_name;
echo '<br />';
echo $cont->data->mobile;
echo '<br />';
echo $cont->data->PIN;
The way you tried to access your message variable applies to objects so you have to remove the true
option from json decode so you create an object and not an array.
Also json_decode
creates an array or an object (based on option TRUE , FALSE) so your third line is unnecessary
Further more i provide you help on how to echo the values you need.
Output is:
Peter
Rick
09887
1234