I am working a project which uses theyworkforyou's API (UK-based political data organisation) and specifically the function getMPInfo() which takes the ID of the MP (member of parliament) and returns lots of data about them (when they came into office, how long they've been in office, etc.)
The problem I'm having is that the following line of code works on every ID I've tried except one (that I've found so far):
var_dump(json_decode(file_get_contents("http://www.theyworkforyou.com/api/getMPInfo?key=MY_KEY&id=SOME_ID&output=js")));
if I do a json_last_error() call I get JSON_ERROR_UTF8 on that one ID.
The ID in question is 10999. You can try it out in the docs section of their api portal here.
If I just output http://www.theyworkforyou.com/api/getMPInfo?key=MY_KEY&id=10999&output=js to the screen and copy and paste it into a json validator, it seems to pass the test fine. I've tried with 3 so far and none have found a fault.
Can anyone see what the problem is or what I've potentially done wrong? It could be a fault of the API of course but I've never had a problem with any json parsing errors using this API in the past (I've been using it with all MPs in the past for a few years now), it works for all other MP IDs I've tried it with and like I said, it seems to be valid json from the point of view of the online validators.
Thank you for your time.
P.S. I'm not sure how you could replicate my problem without an API key so here is a link to the api portal homepage where you can sign up for an API key.
JSON_ERROR_UTF8 error means
Malformed UTF-8 characters, possibly incorrectly encoded
json_encode says
All string data must be UTF-8 encoded
So you just need to utf8_encode your input before passing it to json_encode.