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);
JSON內容必須採用UTF8編碼。如果您從api獲得的數據不是,您可以在JSON解碼之前自己將其編碼爲UTF8 –
Ps您的示例代碼將URL編碼爲UTF8而不是數據本身 –
@scrowler感謝您的回覆!我如何編碼數據? – Lachie