How can laravel avoid non-object errors?

Source: Internet
Author: User
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_decodeWhen the second parameter is passedtrue.ArrayIf no data is obtained, it is generally returned.nullInstead 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

  1. $ 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.

  2. 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 ')

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.