来自json远程文件的php中的奇怪字符

I know this question is all over the place , and ive been searching for hours now , but I cannot find any solution that works. And trust me , i have tested alot of them...

Whenever there is an icelandic letter in the string it gives me : � �

Ive been trying all solutions here on stackoverflow, and the internet. But I just dont see what im doing wrong.

Here is my setup.

grabber.php has this code inside :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="//code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>

</head>

<?php 
ini_set("display_errors",1);

// attempt to remove strange characters
ini_set('default_charset', 'utf-8');
header('Content-Type: text/html; charset=UTF-8');
mb_internal_encoding('UTF-8');  
mb_http_output('UTF-8'); 
mb_http_input('UTF-8');  
mb_regex_encoding('UTF-8');

$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0
"));
$context = stream_context_create($opts);
// SKRIFA HÉR VÖRUNÚMERIÐ
$ids = '824379-421';
$header = file_get_contents('https://domain.com/getproduct.do?id='.$ids,false,$context);
$json = $header;

$obj = json_decode($json);
$myndin = $obj->{'productImage'};
$voruheiti = $obj->{'productName'};
$verdmedvsk = $obj->{'normalPriceIncVat'};
$verdanvsk = $obj->{'normalPriceNoVat'};
$vorulysing = $obj->{'productDesc'};
echo $ids
?>
<body>
<div class="vorumynd"><img src="<?php echo $myndin ?>"></div>
<div class="voruheiti"><h3>Vöruheiti :</h3> <?php echo $voruheiti ?></div>
<br />
<div class="vorulysing"><h3>Vörulýsing :</h3><?php echo $vorulysing ?></div>
<br />
<div class="verd-container">
<div class="verd verd-an-vsk"><h3>Verð :</h3><b><?php echo $verdanvsk ?></b> án/vsk</div>
<div class="verd verd-med-vsk"><b><?php echo $verdmedvsk ?></b> m/vsk</div>
</div>
<?php


?>


</body>
</html>

When I fetch the remote servers header I get :

Array ( 
[0] => HTTP/1.1 200 OK 
[1] => Server: nginx/1.8.0 
[2] => Date: Thu, 06 Oct 2016 16:52:32 GMT 
[3] => Content-Type: text/html;charset=ISO-8859-1 
[4] => Content-Length: 4352 
[5] => Connection: close 
[6] => Set-Cookie: JSESSIONID=2A03A2B7A92D53CE7C166B4E9DA11DAC; Path=/resellers; Secure; HttpOnly ) Array ( 
[0] => HTTP/1.1 200 OK 
[Server] => nginx/1.8.0 
[Date] => Thu, 06 Oct 2016 16:52:32 GMT 
[Content-Type] => text/html;charset=ISO-8859-1 
[Content-Length] => 4352 
[Connection] => close 
[Set-Cookie] => JSESSIONID=C7DA983AC63F1117865CF98962336CAB; Path=/resellers; Secure; HttpOnly 
)

As soon as I read the question I figured it out.

Sometimes you just need to talk to someone or write down the problem

I had the charset all wrong

I was using utf-8 , but i should have been using ISO-8859-1

Because the remote server header responded with that.

Hope this helps someone out...

To print out the remote server header to see what charset to use, use the following code :

$url = 'https://remote-domain.com/';
print_r(get_headers($url));
print_r(get_headers($url, 1));

Before doing json_decode, you should convert the data from ISO-8859-1 to UTF-8:

$json = mb_convert($json, 'UTF-8', 'ISO-8559-1');

Note that you need the extension mbstring to be installed and enabled in PHP.