json_decode返回null(特殊字符)

时间:2015-06-20 00:11:11

标签: php

我遇到了json_decode的问题。我有一个表单,从列表和字符名称(可以有特殊字母,如:öàé等)询问领域:

<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);

1 个答案:

答案 0 :(得分:3)

我找到了答案!

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

    <meta charset="utf-8">

希望这可以帮助任何有这样问题的人!