2015-06-20 118 views
2

我遇到问题json_decode。我有一个要求从列表中的境界和字符的名称(可以有特殊的字母,如:öàé等):一种形式json_decode返回null(特殊字符)

<form method="post"> 
    <!--//<input name="realm" type="search" id="realm" value="<?php echo $_POST['realm']; ?>"><br />--> 
    <div class="styled-select"> 
    <select name="realm" required> 
     <option value="" <?php if ($selected_choice == "none") { print "selected='selected'"; }?>>Select...</option> 
     <option value="Aegwynn" <?php if ($selected_choice == "Aegwynn") { print "selected='selected'"; } ?>>Aegwynn</option> 
     <option value="Area 52" <?php if ($selected_choice == "Area 52") { print "selected='selected'"; } ?>>Area 52</option> 
     <option value="Arygos" <?php if ($selected_choice == "Arygos") { print "selected='selected'"; } ?>>Arygos</option> 
     <option value="Caelestrasz" <?php if ($selected_choice == "Caelestrasz") { print "selected='selected'"; } ?>> Caelestrasz </option> 
     <option value="Darrowmere" <?php if ($selected_choice == "Darrowmere") { print "selected='selected'"; } ?>> Darrowmere </option> 
     <option value="Dath'Remar" <?php if ($selected_choice == "Dath'Remar") { print "selected='selected'"; } ?>>Dath'Remar</option><!--'--> 
     <option value="Drakkari" <?php if ($selected_choice == "Drakkari") { print "selected='selected'"; } ?>> Drakkari </option> 
     <option value="Dreadmaul" <?php if ($selected_choice == "Dreadmaul") { print "selected='selected'"; } ?>> Dreadmaul </option> 
     <option value="Frostmourne" <?php if ($selected_choice == "Frostmourne") { print "selected='selected'"; } ?>>Frostmourne</option> 
     <option value="Gnomeregan" <?php if ($selected_choice == "Gnomeregan") { print "selected='selected'"; } ?>>Gnomeregan</option> 
     <option value="Gorgonnash" <?php if ($selected_choice == "Gorgonnash") { print "selected='selected'"; } ?>>Gorgonnash</option> 
     <option value="Illidan" <?php if ($selected_choice == "Illidan") { print "selected='selected'"; } ?>>Illidan</option> 
     <option value="Khaz'Goroth" <?php if ($selected_choice == "Khaz'Goroth") { print "selected='selected'"; } ?>>Khaz'Goroth</option><!--'--> 
     <option value="Maiev" <?php if ($selected_choice == "Maiev") { print "selected='selected'"; } ?>> Maiev </option> 
     <option value="Quel'Thalas" <?php if ($selected_choice == "Quel'Thalas") { print "selected='selected'"; } ?>> Quel'Thalas </option><!--'--> 
     <option value="Ragnaros" <?php if ($selected_choice == "Ragnaros") { print "selected='selected'"; } ?>> Ragnaros</option> 
    </select> 
    <input name="char" style="none" type="text" size="20" id="char" value="<?php echo $_POST['char']; ?>" required> 
    </div> 
    <br> 
    <input type="submit" name="Submit" class="btn btn-warning" value="Submit"> 
</form> 

这里就是问题从产生的PHP(我已经做了很多的意见,我曾试图解决这个问题):

$url = 'https://us.api.battle.net/wow/character/' . $formRealm . '/' . $formChar . '?fields=items&locale=en_US&apikey=uv5rmcd7mk326pqxcy7hv79y3z97vwmr'; 
$result = file_get_contents($url); 
$url1 = 'https://us.api.battle.net/wow/character/' . $formRealm . '/' . $formChar . '?fields=stats&locale=en_US&apikey=uv5rmcd7mk326pqxcy7hv79y3z97vwmr'; 
$result1 = file_get_contents($url1); 
//echo $url; 

// file_get_contents call instead 
$data = $result; 
$data1 = $result1; 

// decode JSON 
$json = json_decode($data, true); 
//$json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($url)); 
//$json = json_decode($json); 
$json1 = json_decode($data1,true); 
//$json1 = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($url1)); 
//$json1 = json_decode($json1); 
+2

JSON内容必须采用UTF8编码。如果您从api获得的数据不是,您可以在JSON解码之前自己将其编码为UTF8 –

+0

Ps您的示例代码将URL编码为UTF8而不是数据本身 –

+0

@scrowler感谢您的回复!我如何编码数据? – Lachie

回答

3

我找到答案!

在HTML代码,我忘了添加在顶部:

<meta charset="utf-8"> 

希望这有助于任何人只要有一个这样的问题!