I sent a curl request to a server and get the following results:
{ "data": [
{ "state_msg": "Canceled",
"dpc": null,
"mgcp_call_ids": null,
"code": 487,"mgcp_mgc_ip": null,
"dst_codecs": "G729,telephone-event,H264,H263",
"megaco_mg_ip": null,"src_codecs":
"G729,PCMU,telephone-event,H264,H263",
"q850_state_details": null,
"pid":1383031680,
"call_time": null
.......
How can i parse this into readable fields? I am looking for something like foreach loop, or something similar.
if this were xml i could do
$response->data->state_msg.
Clearly that wont work here. what can i do?
That's a JSON response. Pass the string through json_decode
to receive the object and iterate over it as you would any other object.
Do it like this:
$json_string = '{"data": {"state_msg": "Canceled", "dpc": null, "mgcp_call_ids": null, "code": 487,"mgcp_mgc_ip": null, "dst_codecs": "G729,telephone-event,H264,H263", "megaco_mg_ip": null,"src_codecs": "G729,PCMU,telephone-event,H264,H263", "q850_state_details": null, "pid":1383031680, "call_time": null}');
$obj = json_decode($json_string);
It will turn your JSON into an array and you can read it like this
echo $obj->data->state_msg;