0

所以我想要一种使用数字海洋启动和关闭我的水滴的方法,他们有自己的 API,但我不太确定该怎么做

基本上我希望能够在我的网站上单击一个按钮,服务器启动并显示 JSON 响应。API url 是这个 https://api.digitalocean.com/droplets/?client_id=[your_client_id]&api_key=[your_api_key]

示例输出是这样的:

{“状态”:“OK”,“液滴”:[{“id”:100823,“名称”:“test222”,“image_id”:420,“size_id”:33,“region_id”:1,“backups_active” :假,“ip_address”:“127.0.0.1”,“锁定”:假,“状态”:“活动”“created_at”:“2013-01-01T09:30:00Z”}]}

感谢任何帮助

编辑:这是我要开始工作的代码。

<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
<title>Server Control Panel</title>
</head>
<body>
    <input type="submit" value="Start!" name="submit" id="Startbutton" />
    <input type="submit" value="Stop Server" name "submit2" id"Stopbutton" />
<?php
if (isset($_POST['submit'])) {
$request = 'https://api.digitalocean.com/droplets/377781/power_on/client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE';
$response  = file_get_contents($request);
$jsonobj  = json_decode($response);
echo($response);                
}
?>
<?php
if (isset($_POST['Stopbutton'])) {
$request = 'https://api.digitalocean.com/droplets/377781/shutdown/client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE';
$response  = file_get_contents($request);
$jsonobj  = json_decode($response);
echo($response);                
}
echo("</ul>"); 

?>
</form>
</body>
</html>
4

1 回答 1

1

action您的表单和method属性丢失。您可能还想将输入字段的名称属性重命名为更有意义的名称。

下面是一些改进的代码:

<?php

if (isset($_POST['Startbutton'])) 
{
    $request = 'https://api.digitalocean.com/droplets/377781/startup/client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE';
    $response  = file_get_contents($request);
    $jsonobj  = json_decode($response);
    echo "<pre>"; 
    print_r($jsonobj);       
    echo "</pre>";                 
}

if (isset($_POST['Stopbutton'])) 
{
    $request = 'https://api.digitalocean.com/droplets/377781/shutdown/client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE';
    $response  = file_get_contents($request);
    $jsonobj  = json_decode($response);
    echo "<pre>"; 
    print_r($jsonobj);       
    echo "</pre>";        
}

?>

<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
<title>Server Control Panel</title>
</head>
<body>

<form action="" method="post">
    <input type="submit" value="Start!" name="Startbutton" id="Startbutton" />
    <input type="submit" value="Stop Server" name = "Stopbutton" id"Stopbutton" />
</form>

</body>
</html>

更新:

问题似乎出在您的 API URL 上。

/droplets/377781/startup/client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE

应该:

/droplets/377781/startup/?client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE

?注意后面的缺失/startup/

希望这可以帮助!

于 2013-08-18T08:26:59.790 回答