I'm building an API using PHP.
What do I need to do to recognize a JSON encoded request?
Does it come with a certain request type?
How do I get just the request body so that I can json_decode it?
RFC 4627 recommends the mime type application/json. You can read the request data with php://input .
The mime type for json is application/json
, but I doubt that has caught on yet to add that to responses.
The easiest way to spot a json response is that it will most likely be wrapped in curly braces and have colons after each member. But that still isn't a sure fire way to know it's json.
The easiest way to test it from PHP is to try to json_decode it, if it throws an error, either it's not json, or it's not well-formed json and thus you won't be able to use it anyway.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.