Php 如何在laravel中对返回的响应进行编码
我试图从返回的响应中对一个字段进行编码。使用SMS api发送短信后,我得到如下响应:Php 如何在laravel中对返回的响应进行编码,php,arrays,json,laravel,laravel-4,Php,Arrays,Json,Laravel,Laravel 4,我试图从返回的响应中对一个字段进行编码。使用SMS api发送短信后,我得到如下响应: '{ "ErrorCode":"000", "ErrorMessage":"Success", "JobId":"e51ca2d4-e3e6-415a-97f4-2e9a133f38e7", "MessageData":[ { "Number":"91974ssdsd2", "MessageParts":[
'{
"ErrorCode":"000",
"ErrorMessage":"Success",
"JobId":"e51ca2d4-e3e6-415a-97f4-2e9a133f38e7",
"MessageData":[
{
"Number":"91974ssdsd2",
"MessageParts":[
{
"MsgId":"91974ddddd-0fe367f3b1774e74bf7c06e21cf38482",
"PartId":1,
"Text":"The OTP for your login is uxlvRF"
}
]
}
]
}'
我将此响应转换为以下格式:
'[
"ErrorCode"=>"000",
"ErrorMessage"=>"Success",
"JobId"=>"e51ca2d4-e3e6-415a-97f4-2e9a133f38e7",
"MessageData"=>[
["Number"=>"919747dddddd",
"MessageParts"=>[
[
"MsgId"=>"9197ddddd-0fe367f3b1774e74bf7c06e21cf38482",
"PartId"=>1,
"Text"=>"The OTP for your login is uxlvRF"
]
]
]
]
]'
这是通过使用代码完成的:$triggersms是实际响应
$tgr_edt=str_replace(':', '=>', $triggersms);
$tgr = str_replace('{', '[', $tgr_edt);
$tgrnw = str_replace('}', ']', $tgr);
但是我尝试在我的laravel代码中使用$tgrnw['ErrorCode'],它显示了一个错误
“无效的Offset错误代码”
有人能帮忙吗?乍一看,你收到的回复对我来说就像是JSON 这样做:
$obj = json_decode($triggersms);
echo $obj->ErrorCode;
或
没有倒逗号。。。我刚才添加的只是为了说明为什么不做
json\u decode
而不是str\u replace
:)
$array = json_decode($triggersms, true);
echo $array['ErrorCode'];