Skip to content

Instantly share code, notes, and snippets.

@cp6

cp6/array_return.php

Last active Aug 10, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.