好的,首先,感谢您阅读我的问题,我会尝试添加尽可能多的信息。我目前正在为 Garrysmod 游戏类型制作排行榜,我从游戏模式的编码人员那里获得了一个 API key ,使用该 API key 我可以使用 STEAM_ID 提取 JSON 编码的数组
示例: http://www.revoltgaming.com/api/?key=SENCORED_API_KEY&action=ajack_all&steamid=STEAM_0:0:5898349
如果我使用下面的代码一切正常:
$usr_url_everything = "http://www.revoltgaming.com/api/?key=114d788c3e40e91842f945f19c978e66&action=ajack_all&steamid=" . $_SESSION['STEAMID'];
$json_encoded = file_get_contents($usr_url_everything);
$json_decoded = json_decode($json_encoded, true);
但我所做的是,当用户第一次加入时,我提取他的 JSON 字符串并将其保存在 MySQL 数据库中:
$usr_url_everything = "http://www.revoltgaming.com/api/?key=114d788c3e40e91842f945f19c978e66&action=ajack_all&steamid=" . $_SESSION['STEAMID'];
$json_encoded = file_get_contents($usr_url_everything);
$query = "INSERT INTO vm_leaderboard (`txtUser`, `txtJson`) VALUES ('" . $_SESSION['STEAM64'] . "', '" . $json_encoded . "')";
但是从我尝试DECODE 我从数据库中提取的 json 字符串的那一刻起,它就不起作用了,它返回 NULL。我保存在数据库中的 json 字符串与我直接从 API 页面获取的字符串相同。
以下是从数据库中提取的字符串: http://www.ishots-cave.com/revolt_/lb_applejack.php
最佳答案
因此,正如我在评论中发布的那样,将其转换为有用的答案——JSON 字符串在插入数据库之前需要进行清理:
$usr_url_everything = "http://www.revoltgaming.com/api/?key=&action=ajack_all&steamid=" . $_SESSION['STEAMID'];
$json_encoded = file_get_contents($usr_url_everything);
// sanitize the JSON string
$json_encoded = mysql_real_escape_string($json_encoded);
// insert in common way
$query = "INSERT INTO vm_leaderboard (`txtUser`, `txtJson`) VALUES ('" . $_SESSION['STEAM64'] . "', '" . $json_encoded . "')";
尽情享受吧!
关于php - JSON 从 URL 而不是从 MySQL 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11051164/