How to call do api with php?

  • Posted December 25, 2015
  • PHP

In the manual

curl -X GET -H ‘Content-Type: application/json’
-H ‘Authorization: Bearer ‘$TOKEN’’

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?


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.

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

Reference: cURL options: Examples:

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

//setup the request, you can also use CURLOPT_URL
$ch = curl_init('');

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