【问题标题】:GET info from external API/URL using PHP使用 PHP 从外部 API/URL 获取信息
【发布时间】:2022-02-01 17:05:12
【问题描述】:

我的 URL http://api.minetools.eu/ping/play.desnia.net/25565 输出我的服务器的统计信息。

例如:

{
  "description": "A Minecraft Server",
  "favicon": null,
  "latency": 64.646,
  "players": {
    "max": 20,
    "online": 0,
    "sample": []
  },
  "version": {
    "name": "Spigot 1.8.8",
    "protocol": 47
  }
}

我想获取在线玩家计数的值以在我的网站上显示为:在线玩家:在线数量

谁能帮忙?

我尝试过:

<b> Online players: 

<?php
$content = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
echo ($content, ["online"]);
}
?>
</b>

但是没有用。

【问题讨论】:

    标签: php


    【解决方案1】:

    1) 不要使用file_get_contents()(如果你能帮忙的话)

    这是因为您需要 enable fopen_wrappers 以启用 file_get_contents() 以处理外部源。有时这是关闭的(取决于您的主机;例如共享主机),因此您的应用程序会中断。

    一般来说,一个不错的选择是curl()

    2) 使用curl() 执行GET 请求

    这很简单。使用curl() 发出带有一些标头的GET 请求。

    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "http://api.minetools.eu/ping/play.desnia.net/25565",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    

    3) 使用响应

    回复以JSON object 形式返回。我们可以使用json_decode() 将其放入可用的对象或数组中。

    $response = json_decode($response, true); //because of true, it's in an array
    echo 'Online: '. $response['players']['online'];
    

    【讨论】:

    • @hd,我正在使用代理连接到互联网。 file_get_content 有效,但 curl 选项会引发“缓存访问被拒绝”。和“抱歉,在您对自己进行身份验证之前,目前不允许您从此缓存中请求 freegeoip.net/json/X.X.X.X。”
    【解决方案2】:

    您的服务器正在返回一个 JSON 字符串。 因此,您应该使用 json_decode() 函数将其转换为普通的 PHP 对象。 此后,您可以访问该对象的任何变量。

    所以,这样的事情会有所帮助

    <?php
    $content =     file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
    
    $result  = json_decode($content);
    
    print_r( $result->players->online );
    ?>
    

    更多关于 json_decode 的细节可以在这里阅读 - http://php.net/manual/en/function.json-decode.php

    【讨论】:

      【解决方案3】:

      您的网络服务(URL:http://api.minetools.eu/ping/play.desnia.net/25565)返回JSON

      这是一种标准格式,PHP(至少从 5.2 起)支持原生解码 - 你会从中得到某种形式的 PHP 结构。

      您的代码当前不起作用(您的语法在 echo 上毫无意义 - 即使它是有效的,您也将原始 JSON 数据的字符串副本视为一个数组 - 这不起作用) ,您需要先让 PHP 解释(解码)JSON 数据:

      http://php.net/manual/en/function.json-decode.php

      <?php
      $statisticsJson = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
      $statisticsObj = json_decode($statisticsJson);
      

      如果发生错误,您的 $statisticsObj 将是 NULL - 您可以使用其他标准 PHP 函数获取该错误:

      http://php.net/manual/en/function.json-last-error.php

      假设它不是 NULL,您可以使用 var_dump($statisticsObj) 检查对象的结构 - 然后更改您的代码以适当地打印出来。

      简而言之,类似于:

      <?php
      $statisticsJson = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
      $statisticsObj = json_decode($statisticsJson);
      if ($statisticsObj !== null) {
         echo $statisticsObj->players->online;
      } else {
         echo "Unknown";
      }
      

      您还应该检查从file_get_contents() 返回的内容 - 各种返回值可能会在错误时返回(这会炸毁json_decode())。有关可能性,请参阅文档:

      http://php.net/manual/en/function.file-get-contents.php

      我还将整个内容包装在一个函数或类方法中,以保持代码整洁。一个简单的“几乎完整”的解决方案可能如下所示:

      <?php
      function getServerStatistics($url) {
          $statisticsJson = file_get_contents($url);
          if ($statisticsJson === false) {
             return false;
          }
      
          $statisticsObj = json_decode($statisticsJson);
          if ($statisticsObj !== null) {
             return false;
          }
      
          return $statisticsObj;
      }
      
      // ...
      
      $stats = getServerStatistics($url);
      if ($stats !== false) {
          print $stats->players->online;
      }
      

      如果您想更好地处理服务器/HTTP 错误等,我会考虑使用 curl_*() - http://php.net/manual/en/book.curl.php

      理想情况下,您还应该确认从您的网络服务返回的结构是您所期望的,然后再盲目地做出假设。您可以使用 property_exists() 之类的东西来做到这一点。

      黑客愉快!

      【讨论】:

      • 很好,虽然改进了 getServerStatistics() 函数。 $url 未被使用。
      【解决方案4】:

      由于它返回一个数组,你应该使用 print_r 或 var_dump 而不是 echo。或者它可能会给你一个错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-13
        • 2018-10-21
        • 2014-04-28
        • 1970-01-01
        • 1970-01-01
        • 2016-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多