Jasper
Jasper

Reputation: 5

Making a request to an API in php

So I want a way to start up and shutdown my droplet with Digital ocean, they have their own API but im not quite sure how to do it

basically I want to be able to click a button on my website, The server starts up and the JSON response is displayed. The API url is this https://api.digitalocean.com/droplets/?client_id=[your_client_id]&api_key=[your_api_key]

and the example output is this:

{ "status": "OK", "droplets": [ { "id": 100823, "name": "test222", "image_id": 420, "size_id":33, "region_id": 1, "backups_active": false, "ip_address": "127.0.0.1", "locked": false, "status": "active" "created_at": "2013-01-01T09:30:00Z" } ] }

Any help is apreciated

EDIT: This is the code I'm trying to get to work.

<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>

Upvotes: 0

Views: 2606

Answers (1)

Amal Murali
Amal Murali

Reputation: 76646

Your form is missing action and method attributes. You might also want to rename the name attributes of your input fields to something more meaningful.

Here's the code with some improvements:

<?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>

UPDATE:

The issue seems to be with your API URL.

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

should be:

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

Notice the missing ? after /startup/.

Hope this helps!

Upvotes: 1

Related Questions