Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
json远程文件中php中的奇怪字符_Php_Json - Fatal编程技术网

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

json远程文件中php中的奇怪字符,php,json,Php,Json,我知道这个问题到处都是,我已经找了好几个小时了,但是我找不到任何有效的解决办法。相信我,我已经测试了很多 只要字符串中有冰岛字母,它就会给我:� � 我一直在尝试stackoverflow和internet上的所有解决方案。但是我不知道我做错了什么 这是我的设置 grabber.php中包含以下代码: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&g

我知道这个问题到处都是,我已经找了好几个小时了,但是我找不到任何有效的解决办法。相信我,我已经测试了很多

只要字符串中有冰岛字母,它就会给我:� �

我一直在尝试stackoverflow和internet上的所有解决方案。但是我不知道我做错了什么

这是我的设置

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>

我一看这个问题就明白了

有时候你只需要和某人谈谈或写下问题

我把字符都搞错了

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

因为远程服务器标头对此进行了响应

希望这能帮到别人

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

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

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

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

请注意,您需要在PHP中安装并启用扩展名
mbstring

我现在唯一的问题是,我的html现在正在输出� �在同一个文件中是否可能有2个不同的字符集,,,一个用于php,一个用于我的html文档?还是有更简单的解决方案?
$json = mb_convert($json, 'UTF-8', 'ISO-8559-1');