如何在php函数中检查json响应中的成功或错误

时间:2015-07-20 09:29:15

标签: php json

Json响应返回

HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json
Date: Mon, 20 Jul 2015 09:24:20 GMT {"success":true}`

HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json 
Date: Mon, 20 Jul 2015 09:24:20 GMT {"error":"Trying to get property of non-object"}

如何编写php代码来检查是否存在成功或错误。

像这样做

$abc=$this->store($values,$reallocation_type,'multiple');

                    if(strpos($abc,'success') !== false)
                    {
                        echo "YES";
                    }

1 个答案:

答案 0 :(得分:1)

这可能就是你要找的东西。

$response = @json_decode($abc);
if ($response && true === $response->success) {
    // success
} else {
    //failure
}
相关问题