Question

How to call do api with php?

  • Posted December 25, 2015
  • PHP

In the manual https://www.digitalocean.com/community/tutorials/how-to-use-and-understand-action-objects-and-the-digitalocean-api

curl -X GET -H ‘Content-Type: application/json’
-H ‘Authorization: Bearer ‘$TOKEN’’
https://api.digitalocean.com/v2/actions/36805022

This will return the requested action object:

{ “action”: { “id”: 36805022, “status”: “completed”, “type”: “snapshot”, “started_at”: “2014-11-14T16:34:39Z”, “completed_at”: “2014-11-14T16:38:52Z”, “resource_id”: 3164450, “resource_type”: “droplet”, “region”: “nyc3” } }

How to get the same output with pure php code?

Subscribe
Share

Hi dude, i have also find out one good example Curl GET And POST Method Calls – PHP Example


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

There is already a library that you can call to interact with the DO v2 API.

You can find it below on GitHub and should be well documented.

https://www.digitalocean.com/community/projects/api-v2-client-in-php https://github.com/toin0u/DigitalOceanV2

If you still want to write pure PHP code, you can use the curl libraries within PHP to make the calls.

Reference: http://php.net/manual/en/book.curl.php cURL options: http://php.net/manual/en/function.curl-setopt.php Examples: http://php.net/manual/en/curl.examples.php

A great example function translated from the CLI cURL you have in your question is provided below as well.

<?php
//setup the request, you can also use CURLOPT_URL
$ch = curl_init('https://api.digitalocean.com/v2/actions/36805022');

// Returns the data/output as a string instead of raw data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Good practice to let people know who's accessing their servers. See https://en.wikipedia.org/wiki/User_agent
curl_setopt($ch, CURLOPT_USERAGENT, 'YourScript/0.1 (contact@email)');

//Set your auth headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $TOKEN
    ));

// get stringified data/output. See CURLOPT_RETURNTRANSFER
$data = curl_exec($ch);

// get info about the request
$info = curl_getinfo($ch);

// close curl resource to free up system resources 
curl_close($ch)
?>