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

时间:2016-10-06 16:57:58

标签: php json

我知道这个问题到处都是,我一直在寻找几个小时,但我找不到任何有效的解决方案。相信我,我已经测试了很多......

每当字符串中有冰岛字母时,它就会给我:��

我一直在stackoverflow和互联网上尝试所有解决方案。但我只是不明白我做错了什么。

这是我的设置。

grabber.php里面有这个代码:

<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\r\n"));
$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>

当我获取远程服务器头时,我得到:

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 
)

2 个答案:

答案 0 :(得分:0)

在执行json_decode之前,您应该将数据从ISO-8859-1转换为UTF-8:

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

请注意,您需要在PHP中安装和启用扩展程序mbstring

答案 1 :(得分:-1)

一读到这个问题,我就知道了。

有时您只需要与某人交谈或写下问题

我的字符集错了

我使用的是utf-8,但我应该使用ISO-8859-1

因为远程服务器头响应了这一点。

希望这有助于某人...

要打印远程服务器标头以查看要使用的字符集,请使用以下代码:

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