3

I am trying to fetch an external JSON response with my PHP back-end But my problem is that the external endpoint is returned as the Content-Type: text/plain;charset=utf-8, and that only gives me gibberish when I read it.

string '����������ݒ�J�� ... etc...

Is there a way to encode that response?

This is what I have done:

$response = file_get_contents('external_url');

I have also tried this:

$json = json_decode(file_get_contents('external_url'), true);
2
  • Any errors on processing the result? If you only display the data, have you set the document to utf-8? header('Content-Type: text/html; charset=UTF-8'); Commented Aug 31, 2015 at 7:09
  • If I use the json_decode I get null and if I set the header above there is no difference. Commented Aug 31, 2015 at 7:18

1 Answer 1

3

It doesn't really matter to PHP what Content-Type the response declares, it doesn't do anything with that information. You're getting exactly the same response body whether the header says text/plain or JSON.

More likely the response is compressed and you need to uncompress it with gzinflate or such.

Sign up to request clarification or add additional context in comments.

3 Comments

If I do that I only get an error... "gzinflate(): data error" These are the content types from the orginal response: Content-Encoding:gzip Content-Length:344662 Content-Type:text/plain;charset=utf-8
Ok, I can set you as the correct answer, since you really pointed me in the right direction, I had to use readgzfile. If you want you can edit that in to your answer :)
If you have to use readgzfile that means you're storing the content in a file... which is quite different usage than gzinflate...?! Anyway, the manual page of gzinflate I link to links to the other functions, and I explicitly left it up to you to figure out precisely which one is the appropriate one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.