Jump to content

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?

Edited by thewhitewalter
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().

Edited by vinny42

 

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 by jazzman1

 


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;
 
?>
 
Edited by PaulRyan
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.