我正在编写一个PHP脚本,我想让它从steam api中获取这个脚本:Steam api example
这是我到目前为止的代码:
<!DOCTYPE html>
<html>
<head>
<title>
Test
</title>
</head>
<body style="text-align:center;">
<form method="post">
<input type="submit" value="OK" name="kk"/>
</form>
<?php
if(isset($_POST['kk'])) {
$json = file_get_contents('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=STEAM API KEY HERE');
$obj = json_decode($json);
echo $obj->result;
}
?>
</head>
</html>我怎么才能拿到它?
发布于 2021-06-15 02:03:03
根据您的评论,您正在尝试回显stdClass。这就是错误的原因。
您可以像这样访问数据(在所附图像中用红色标记)
<?php
if(isset($_POST['kk'])) {
$json = file_get_contents('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=STEAM API KEY HERE');
$obj = json_decode($json);
echo $obj->result->services->SessionsLogon;
echo $obj->result->services->SteamCommunity;
echo $obj->result->services->IEconItems;
}如果您更喜欢使用数组而不是stdClass,您可以使用$obj = json_decode($json, true),并使用echo $obj['result']['services']['SessionsLogon']访问元素,等等。
要获得人类可读的stdClass或数组输出,您应该使用pre和print_r,而不仅仅是var_dump。例如。echo '<pre>' . print_r($obj, true) . '</pre>';
https://stackoverflow.com/questions/67974337
复制相似问题