When laravel calls a third-party api, it is too dependent on the third-party data structure. If the third-party structure is slightly adjusted, an error is reported on the webpage. Possible data structures: 1. {& #039; code & #039;: 200, & #039; result & #039 ;:{& #039; list & #039;: [& #039; name & #039;: & #039; hello & #039;, & #039; age & #039;: 18], [& #039; name & #039 ;: & #039; world & #039;]} 2. {& #039; cod... when laravel calls a third-party api, it is too dependent on the third-party data structure. If the third-party structure is slightly adjusted, an error is reported on the webpage.
Possible data structures:
1. {'code': 200, 'result': {'LIST': ['name': 'hello', 'age': 18], ['name ': 'World']}
2. {'code': 204, 'result': 'wrong '}
3. data cannot be obtained due to a network exception.
$ Data-> code; an error is returned if a network exception occurs.
$ Data-> result-> list; if the data is abnormal, an error is returned.
End ($ data-> result-> list)-> age; if an element in the list does not contain age, an error is returned (this is the normal situation of data)
How can I configure or encode it to improve laravel's fault tolerance? (For example, native php has a relatively high fault tolerance, and all the elements that cannot be found are left blank, so no error is reported .)
Reply content:
When laravel calls a third-party api, it is too dependent on the third-party data structure. If the third-party structure is slightly adjusted, an error is reported on the webpage.
Possible data structures:
1. {'code': 200, 'result': {'LIST': ['name': 'hello', 'age': 18], ['name ': 'World']}
2. {'code': 204, 'result': 'wrong '}
3. data cannot be obtained due to a network exception.
$ Data-> code; an error is returned if a network exception occurs.
$ Data-> result-> list; if the data is abnormal, an error is returned.
End ($ data-> result-> list)-> age; if an element in the list does not contain age, an error is returned (this is the normal situation of data)
How can I configure or encode it to improve laravel's fault tolerance? (For example, native php has a relatively high fault tolerance, and all the elements that cannot be found are left blank, so no error is reported .)
For multi-layer data, it is indeed difficult to use the object-oriented method. It is better to change it to array for access-injson_decode
When the second parameter is passedtrue
.Array
If no data is obtained, it is generally returned.null
Instead of reporting an error.
$data['code']
If a network exception occurs =>null
$data['result']['list']
If the data is abnormal, an error is returned.null
end($data['result']['list'])['age']
If an element in the list does not contain age =>null
$ Data-> result-> list; if the data is abnormal, an error is returned.
The non-object exception is reported here. The solution is through object_get ($ data, 'result. list', 'null'); the main method is the object_get method. You can check the source code.
End ($ data-> result-> list)-> age; if an element in the list does not contain age, an error is returned (this is the normal situation of data ).
The Undefined property exception is reported here. The solution is the same as object_get (end ($ data-> result-> list), 'age', and 'null ')