简体   繁体   中英

json_decode returns null (special characters)

I'm having issues with json_decode . I have a form that asks the realm from a list and the character name (which can have special letters such as: ö , à , é , etc):

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

Here is where the issue is arising from in the PHP (I have made a lot of comments where I have tried to fix the issue):

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

I found an answer!

At the top of the html code I forgot to add in:

    <meta charset="utf-8">

Hope this helps anyone with an issue like this!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM