在php中向API发出请求

在php中向API发出请求,php,api,Php,Api,所以我想用Digital ocean启动和关闭我的液滴,他们有自己的API,但我不知道怎么做 基本上,我希望能够在我的网站上点击一个按钮,服务器启动并显示JSON响应。 API url如下所示https://api.digitalocean.com/droplets/?client_id=[您的\u客户端\u id]&api\u密钥=[您的\u api\u密钥] 示例输出如下: { “状态”:“确定”, “飞沫”:[ { “id”:100823, “名称”:“test222”, “图像id”:4

所以我想用Digital ocean启动和关闭我的液滴,他们有自己的API,但我不知道怎么做

基本上,我希望能够在我的网站上点击一个按钮,服务器启动并显示JSON响应。 API url如下所示https://api.digitalocean.com/droplets/?client_id=[您的\u客户端\u id]&api\u密钥=[您的\u api\u密钥]

示例输出如下:

{ “状态”:“确定”, “飞沫”:[ { “id”:100823, “名称”:“test222”, “图像id”:420, “尺寸id”:33, “区域id”:1, “备份处于活动状态”:false, “ip地址”:“127.0.0.1”, “锁定”:false, “状态”:“活动” “创建时间”:“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>

服务器控制面板

您的表单缺少
操作
方法
属性。您可能还希望将输入字段的名称属性重命名为更有意义的名称属性

下面是一些改进的代码:

<?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>
应该是:

注意
/startup/
之后缺少的


希望这有帮助

你到底遇到了什么问题?好吧,不管我怎么做,我都不能让它工作。你的问题在你的HTML中。您有
名称“submit2”
它应该是等号的
name=“submit2”
而且,PHP测试应该在名称字段上,没有ID字段:
if(isset($\u POST['Stopbutton'))
$\u POST['submit2']是您想要的。祝你好运:)嗯,好吧,我补充说,它仍然不起作用。编辑代码,这样人们可以看到其他错误,如果存在:)/droplets/[droplet_id]/shutdown是我试图使用的。谢谢你这么多,我不敢相信我错过了xD
/droplets/377781/startup/client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE
/droplets/377781/startup/?client_id=CLIENTIDGOESHERE&api_key=APIKEYGOESHERE