Skip to content

Commit 2dcad86

Browse files
author
SoftLayer
committed
Added a code sample file.
1 parent a96f036 commit 2dcad86

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

example.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2010, SoftLayer Technologies, Inc. All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither SoftLayer Technologies, Inc. nor the names of its contributors may
14+
* be used to endorse or promote products derived from this software without
15+
* specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
*/
29+
30+
/**
31+
* Start by including the API client class. This example assumes that the
32+
* SoftLayer API classes are in the directory "SoftLayer" relative to this
33+
* script's path.
34+
*
35+
* If you wish to use the XML-RPC API then replace mentions of
36+
* SoapClient.class.php with XmlrpcClient.class.php and SoftLayer_SoapClient
37+
* with SoftLayer_XmlrpcClient.
38+
*/
39+
require_once dirname(__FILE__) . '/SoftLayer/SoapClient.class.php';
40+
41+
/**
42+
* It's possible to define your SoftLayer API username and key diredtly in the
43+
* class file, but it's far easier to define them before creating your API
44+
* client.
45+
*/
46+
$apiUsername = 'set me';
47+
$apiKey = 'set me too';
48+
49+
/**
50+
* Usage:
51+
* SoftLayer_SoapClient::getClient([API Service], <object id>, [username], [API key]);
52+
*
53+
* API Service: The name of the API service you wish to connect to.
54+
* id: An optional id to initialize your API service with, if you're
55+
* interacting with a specific object. If you don't need to specify
56+
* an id then pass null to the client.
57+
* username: Your SoftLayer API username.
58+
* API key: Your SoftLayer API key,
59+
*/
60+
$client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
61+
62+
/**
63+
* Once your client object is created you can call API methods for that service
64+
* directly against your client object. A call may throw an exception on error,
65+
* so it's best to try your call and catch exceptions.
66+
*
67+
* This example calls the getObject() method in the SoftLayer_Account API
68+
* service. <http://sldn.softlayer.com/wiki/index.php/SoftLayer_Account::getObject>
69+
* It retrieved basic account information, and is a great way to test your API
70+
* account and connectivity.
71+
*/
72+
try {
73+
print_r($client->getObject());
74+
} catch (Exception $e) {
75+
die($e->getMessage());
76+
}
77+
78+
/**
79+
* For a more complex example we’ll retrieve a support ticket with id 123456
80+
* along with the ticket’s updates, the user it’s assigned to, the servers
81+
* attached to it, and the datacenter those servers are in. We’ll retrieve our
82+
* extra information using a nested object mask. After we have the ticket we’ll
83+
* update it with the text ‘Hello!’.
84+
*/
85+
86+
// Declare an API client to connect to the SoftLayer_Ticket API service.
87+
$client = SoftLayer_SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey);
88+
89+
// Assign an object mask to our API client:
90+
$objectMask = new SoftLayer_ObjectMask();
91+
$objectMask->updates;
92+
$objectMask->assignedUser;
93+
$objectMask->attachedHardware->datacenter;
94+
$client->setObjectMask($objectMask);
95+
96+
// Retrieve the ticket record.
97+
try {
98+
$ticket = $client->getObject();
99+
} catch (Exception $e) {
100+
die('Unable to retrieve ticket record: ' . $e->getMessage());
101+
}
102+
103+
// Now update the ticket.
104+
$update = new stdClass();
105+
$update->entry = 'Hello!';
106+
107+
try {
108+
$update = $client->addUpdate($update);
109+
echo "Updated ticket 123456. The new update's id is " . $update->id . '.';
110+
} catch (Exception $e) {
111+
die('Unable to update ticket: ' . $e->getMessage();
112+
}

0 commit comments

Comments
 (0)