Skip to content

Instantly share code, notes, and snippets.

@cp6
Last active August 10, 2020 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cp6/e6fac90d9cd6e78ecd068a71d24cdacb to your computer and use it in GitHub Desktop.
Save cp6/e6fac90d9cd6e78ecd068a71d24cdacb to your computer and use it in GitHub Desktop.
Vultr instance id for instance label API v2
<?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);
}
<?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