简体   繁体   English

在PHP中使用REST API

[英]Use REST API in PHP

i was trying to google but I did'nt see something that helped me. 我正在尝试使用google,但没有看到任何对我有所帮助的东西。

i'm using the Planetteamspeak API. 我正在使用Planetteamspeak API。 For Example, I can do: 例如,我可以这样做:

GET https://api.planetteamspeak.com/serverstatus/1.2.3.4:9987/

An example output is: 示例输出为:

    {
  "status": "success",
  "result": {
    "name":      "Planet TeamSpeak",
    "users":     91,
    "slots":     512,
    "online":    true,
    "password":  false
  }
}

Can anyone give me a simple way to show this in an HTML/PHP table like this? 谁能给我一种简单的方法来在这样的HTML / PHP表中显示它吗?

http://cdn.treudler.net/shared/screenshots/capture_20-01-2015-05-56-27.png http://cdn.treudler.net/shared/screenshots/capture_20-01-2015-05-56-27.png

I'm a noob in Restful API's :c thank you so much!!! 我是Restful API的菜鸟:c非常感谢!!!

<?php 
    $server = $_GET['serverstatus'];
    $result = file_get_contents('https://api.planetteamspeak.com/serverstatus/'.$server.'/');
    $result = json_decode($result);
?>

<h2>Server status</h2>
<table border=1>
    <tr><td>Status</td><td><?=$result->status;?></td></tr>
    <tr><td>Name</td><td><?=$result->result->name;?></td></tr>
    <tr><td>Users</td><td><?=$result->result->users;?></td></tr>
    <tr><td>Slots</td><td><?=$result->result->slots;?></td></tr>
    .....
</table>

<!-------------------------------------------------------->

<?php 
    $server = $_GET['servernodes'];
    $result = file_get_contents('https://api.planetteamspeak.com/servernodes/'.$server.'/');
    $result = json_decode($result);
?>

<h2>Server Nodes</h2>
<table border=1>
    <tr><td>Status</td><td><?=$result->status;?></td></tr>
    <tr><td>Name</td><td><?=$result->result->name;?></td></tr>
    <tr><td>Users</td><td><?=$result->result->users;?></td></tr>
    <tr><td>Slots</td><td><?=$result->result->slots;?></td></tr>
    .....
</table>

<!-------------------------------------------------------->
.... add as many variables / sections as you wish

The way to call the script: 调用脚本的方式:
https://ovh.treudler.net/api/index.php?serverstatus=1.2.3.4:9987&servernodes=1.4.6.8:9987 https://ovh.treudler.net/api/index.php?serverstatus=1.2.3.4:9987&servernodes=1.4.6.8:9987

Keep adding &variable=value to the URL as many times as you please 不断向您的网址添加&variable=value的次数

I'm assuming $response is the response back from the rest call. 我假设$ response是其余调用返回的响应。

    <?php
 $response= file_get_contents('https://api.planetteamspeak.com/serverstatus/82.211.30.15:9987/%22');
$response = json_decode($response);
?>

    <table style="width:100%">
  <tr>
    <td>General Information</td>
  </tr>
  <tr>
    <td>Name:  <?php echo $response->result->name;?></td>
  </tr>
  <tr>
    <td>Status:  <?php if($response->result->online == true){echo "Online";}else{echo "Offline";}?></td>
  </tr>
  <tr>
    <td>Users Online:  <?php echo $response->result->users;?></td>
  </tr>
  <tr>
    <td>Users Online:  <?php echo $response->result->users;?></td>
  </tr>
  <tr>
    <td>Flags:  </td>
  </tr>
 <tr>
    <td>Last Update: <?php echo getdate()?> </td>
  </tr>
</table> 
<?php $result = json_decode($data,true)?>

<table border=1>
    <tr><td>Status</td><td><?php echo $result['status'];?></td></tr>
    <tr><td>Name</td><td><?php echo $result['name'];?></td></tr>
    <tr><td>Users</td><td><?php echo $result['users'];?></td></tr>
    <tr><td>Slots</td><td><?php echo $result['slots'];?></td></tr>
    .....
</table>

I can't comment but I've been reading your comments here and realize that you may not actually know how to send the request itself in order to receive the data in the first place. 我无法发表评论,但我一直在这里阅读您的评论,并意识到您可能实际上并不真正知道如何发送请求本身以首先接收数据。 There are a few ways which are outlined pretty well in answers to this question. 在回答这个问题时,有几种方法可以很好地概述。 How to send a GET request from PHP? 如何从PHP发送GET请求?

Planetteamspeak API returns Data in Json Format Planetteamspeak API以Json格式返回数据

you can do like this to get json data to array 您可以像这样将json数据获取到数组

$resultData = json_decode($data);

and then use HTML table and display data in it with PHP 然后使用HTML表并通过PHP在其中显示数据

echo $resultData->result->name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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