This is probably a really simple question, but I just can’t find any info on it.
I work with a system that aggregates a lot of data from various sources and then stores that data in a database. For the most part, the system works fine, but occasionally we get an issue where data can have awkward character encoding (for instance, when the data is in another language, like French) that our system doesn’t like.
The data gets passed to our processing server (we use Gearman), and to ensure that all the info pertaining to source gets passed we json_encode an array with everything we need. My question to you is: if I wrap the json_encode in a try/catch block, will things that cause “PHP Warning: json_encode(): Invalid UTF-8 sequence in argument” messages trigger the catch block to activate?
Thanks!
No but you can check it’s return value in a function and throw an exception when something goes wrong. You can also use
json_last_errorto get details on the errorExample:
I do find it highly annoying that to get the actual error message you have to check a list of constants that is returned from
json_last_error(). In the past I’ve used a switch / case statement to make that happen but you could throw different exceptions depending on the error.