Last active
August 10, 2020 03:21
-
-
Save cp6/e6fac90d9cd6e78ecd068a71d24cdacb to your computer and use it in GitHub Desktop.
Vultr instance id for instance label API v2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function idForLabel(string $label, array $instance_data) | |
{ | |
foreach ($instance_data['instances'] as $instance) { | |
if ($instance['label'] == $label) { | |
return array('found' => true, 'id' => $instance['id'], 'searchLabel' => $label); | |
} | |
} | |
return array('found' => false, 'searchLabel' => $label); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function instancesCall() | |
{ | |
$api_key = 'VULTR-API-KEY-HERE'; | |
$crl = curl_init('https://api.vultr.com/v2/instances'); | |
curl_setopt($crl, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($crl, CURLOPT_HTTPHEADER, array("Authorization: Bearer $api_key")); | |
curl_setopt($crl, CURLOPT_ENCODING, 'gzip,deflate'); | |
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); | |
$call_response = curl_exec($crl); | |
curl_close($crl); | |
return json_decode($call_response, true); | |
} | |
function idForLabel(string $label, array $instance_data) | |
{ | |
foreach ($instance_data['instances'] as $instance) { | |
if ($instance['label'] == $label) { | |
return $instance['id']; | |
} | |
} | |
return "No id found for $label"; | |
} | |
echo idForLabel('label to search id for', instancesCall()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment