Jump to content

How to handle this PHP encoding issue?


thewhitewalter

Recommended Posts

I'm using PHP to grab some JSON from this URL - goo.gl/xdIqEy

 

I've tried using cURL and file_get_contents(), but the 'LastName' element always comes back looking like ─Éur─æi─ç

 

If I view that URL in Chrome, it looks like ÄurÄ‘ić

 

It should look like Đurđić

 

It's obviously some kind of encoding issue, but how do I handle this? The HTTP response headers don't give any clues to the encoding type.

 

I've tried a lot of different iconv combinations when I've got the string back in PHP - but no luck.

 

If I go to that URL in IE, it let's me download the .json file to disk. When I open that in Sublime Text, it looks correct.

 

Any advice?

Link to comment
https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/
Share on other sites

Try to set up apache headers using a php header function. So put it on the top of this page where you're running a cURL script. 

header('Content-Type: application/json; charset=utf-8');

There is no problem for me with this URL, running the script into a linux terminal using a curl library.

Why?

 

 

More important is why not: because the characters used in the example do not exist in latin1.

 

Back to the question:

 

 

If I view that URL in Chrome, it looks like

 

Because you are looking at an UTF8 string while the browser thinks it's Latin1.

 

If you add a header like

Content-Type: text/html; charset=UTF-8

you should see things correctly.

 

Read the PHP manual about header().

 

More important is why not: because the characters used in the example do not exist in latin1.

 

Because the remote machine is already encoded these unicode characters as utf8!

Everything you should have to do is to set a proper charset to the file.

 

 

If you add a header like

Content-Type: text/html; charset=UTF-8

 

Why do you want to output this JSON object as a Content-Type: text/html?

 


Because the remote machine is already encoded these unicode characters as utf8!

 

The suggestion was utf8_decode), not utf8_encode().

 

 


Everything you should have to do is to set a proper charset to the file.

 

If you just want to repeat the content from the remote server, then yes, add a header and you see the data again.

Of course the next step is using the data and that's more complicated in utf8 in PHP.

 

 


Why do you want to output this JSON object as a Content-Type: text/html?

 

I don't, I missed the part that said this was JSON, and just quickly copied from the manual that I also told the OP to read.

*Edit - Back track, need to check something.

 

*Edit edit - Try the following:

 

<?PHP

  header('Content-Type: application/json; charset=utf-8');

  $contents = file_get_contents('http://cdn.content.easports.com/fifa/fltOnlineAssets/C74DDF38-0B11-49b0-B199-2E2A11D1CC13/2014/fut/items/web/198611.json');
 
  echo $contents;
 
?>
 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.