Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

271
Visualizações
json_decode JSON_ERROR_UTF8 on one API call, passes validation test when copy/pasted and all other API calls work fine

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda