Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created June 28, 2010 21:33
Show Gist options
  • Save softlayer/456390 to your computer and use it in GitHub Desktop.
Save softlayer/456390 to your computer and use it in GitHub Desktop.
<?php
/**
* Get the recurring cost of a single server or all servers on your account.
*
* Perform two operations:
* 1) Get a list of servers on a SoftLayer account along with their recurring
* monthly costs. We can get that by calling getHardware() in the
* SoftLayer_Account API service with an object mask to retrieve cost.
* 2) Get the cost for a single server by calling getCost() in the
* SoftLayer_Hardware_Server service.
*
* See below for more details.
*
* This assumes the SoftLayer API PHP client
* <http://github.com/softlayer/softlayer-api-php-client> is installed in the
* directory '/SoftLayer' in this script's path and that you wish to use the
* SOAP client instead of our XML-RPC client.
*
* @see http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware
* @see http://sldn.softlayer.com/reference/services/SoftLayer_Hardware_Server/getCost
* @license <http://sldn.softlayer.com/article/License>
* @author SoftLayer Technologies, Inc. <[email protected]>
*/
// Include XmlrpcClient.class.php if you'd like to use our XML-RPC client
// instead.
require_once(dirname(__FILE__) . '/SoftLayer/SoapClient.class.php');
/**
* Your SoftLayer API username and key.
*
* Generate an API key at the SoftLayer Customer Portal:
* https://manage.softlayer.com/Administrative/apiKeychain
*/
$apiUsername = 'set me!';
$apiKey = 'set me too!';
/**
* Method one: Getting the cost for every server on your account.
*
* Create a connection to the SoftLayer_Account API service and call the
* getHardware() method to get a list of hardware on our account. Put an object
* mask in the API call to retrieve the cost associated with hardware to get
* every server's cost along with our hardware records.
*/
$client = Softlayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
// Add the object mask to the call.
$objectMask = new SoftLayer_ObjectMask();
$objectMask->hardware->cost;
$client->setObjectMask($objectMask);
try {
// Retrieve our account's hardware records.
$hardware = $client->getHardware();
print_r($hardware);
} catch (Exception $e) {
die('Unable to retrieve hardware list: ' . $e->getMessage());
}
/**
* Method two: Getting the cost for a single server given the server's id
* number.
*
* Every relational property to a data type in the SoftLayer API has a get
* method in that data type's corresponding service. A server's cost follows
* the same rule. The cost property relational to the SoftLayer_Hardware_Server
* data type is the same value as the result of the getCost() call in the
* SoftLayer_Hardware_Server API service.
*
* Create a connection to the SoftLayer_Hardware_Server service for the given
* server id and call the getCost() method to get its monthly recurring fee.
*/
$serverId = 1234;
// Make a connection to the SoftLayer_Hardware_Server service.
$client = Softlayer_SoapClient::getClient('SoftLayer_Hardware_Server', $serverId, $apiUsername, $apiKey);
try {
// Get our server's cost.
$cost = $client->getCost();
echo 'Server cost is $' . $cost . '/mo.';
} catch (Exception $e) {
die('Unable to retrieve server cost: ' . $e->getMessage());
}
@akankshajain
Copy link

getCost method gives monthly cost of a hardware server? what if i want to get monthly cost of a server before ordering? How can I get monthy cost of individual product items like "Monthly cost of 32GB RAM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment