thewhitewalter Posted September 18, 2013 Share Posted September 18, 2013 (edited) 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? Edited September 18, 2013 by thewhitewalter Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/ Share on other sites More sharing options...
jazzman1 Posted September 18, 2013 Share Posted September 18, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450065 Share on other sites More sharing options...
PravinS Posted September 19, 2013 Share Posted September 19, 2013 try using utf8_decode() function Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450196 Share on other sites More sharing options...
jazzman1 Posted September 19, 2013 Share Posted September 19, 2013 try using utf8_decode() function Why? Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450258 Share on other sites More sharing options...
vinny42 Posted September 19, 2013 Share Posted September 19, 2013 (edited) 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(). Edited September 19, 2013 by vinny42 Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450315 Share on other sites More sharing options...
jazzman1 Posted September 19, 2013 Share Posted September 19, 2013 (edited) 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? Edited September 19, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450319 Share on other sites More sharing options...
vinny42 Posted September 20, 2013 Share Posted September 20, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450367 Share on other sites More sharing options...
PaulRyan Posted September 20, 2013 Share Posted September 20, 2013 (edited) *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; ?> Edited September 20, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/282244-how-to-handle-this-php-encoding-issue/#findComment-1450368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.