Skip to content

Commit 3a3e144

Browse files
author
SoftLayer
committed
Support for SoftLayer's public netwoek endpoints.
The SoftLayer_SoapClient and SoftLayer_XmlrpcClient classes now default to use the public network endpoints. Pass an optional fourth parameter to their getClient() methods specifying the API endpoint you wish to use. The SOAP and XML-RPC classes have constants defined that define both the public and private network endpoints for the SOAP and XML-RPC APIs: SoftLayer_SoapClient::API_PUBLIC_ENDPOINT SoftLayer_SoapClient::API_PRIVATE_ENDPOINT SoftLayer_XmlrpcClient::API_PUBLIC_ENDPOINT SoftLayer_XmlrpcClient::API_PRIVATE_ENDPOINT Just like authentication you can define the API_BASE_URL constant in the client classes if you wish to use a single API endpoint for every API call you wish to make.
1 parent 891ea92 commit 3a3e144

File tree

4 files changed

+108
-20
lines changed

4 files changed

+108
-20
lines changed

README.textile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ h2. Overview
44

55
The SoftLayer API PHP client classes provide a simple method for connecting to and making calls from the SoftLayer API and provides support for many of the SoftLayer API's features. Method calls and client management are handled by the PHP SOAP and XML-RPC extensions.
66

7-
Currently the SoftLayer API only allows connections from within the SoftLayer private network. The system using this class must be either directly connected to the SoftLayer private network (eg. purchased from SoftLayer) or have access to the SoftLayer private network via a VPN connection.
8-
97
Making API calls using the SoftLayer_SoapClient or SoftLayer_XmlrpcClient classes is done in the following steps:
108

11-
# Instantiate a new SoftLayer_SoapClient or SoftLayer_XmlrpcClient object using the SoftLayer_SoapClient::getClient() or SoftLayer_XmlrpcClient::getClient() methods. Provide the name of the service that you wish to query, an optional id number of the object that you wish to instantiate, your SoftLayer API username, and your SoftLayer API key.
9+
# Instantiate a new SoftLayer_SoapClient or SoftLayer_XmlrpcClient object using the SoftLayer_SoapClient::getClient() or SoftLayer_XmlrpcClient::getClient() methods. Provide the name of the service that you wish to query, an optional id number of the object that you wish to instantiate, your SoftLayer API username, your SoftLayer API key, and an optional API endpoint base URL. The client classes default to connect over the public Internet. Enter SoftLayer_SoapClient::API_PRIVATE_ENDPOINT or SoftLayer_XmlrpcClient::API_PRIVATE_ENDPOINT to connect to the API over SoftLayer's private network. The system making API calls must be connected to SoftLayer's private network (eg. purchased from SoftLayer or connected via VPN) in order to use the private network API endpoints.
1210
# Define and add optional headers to the client, such as object masks and result limits.
1311
# Call the API method you wish to call as if it were local to your client object. This class throws exceptions if it's unable to execute a query, so it's best to place API method calls in try / catch statements for proper error handling.
1412

@@ -20,7 +18,7 @@ h2. System Requirements
2018

2119
The SoftLayer_SoapClient class requires at least PHP 5.2.3 and the PHP SOAP enxtension installed. The SoftLayer_Xmlrpc class requires PHP at least PHP 5 and the PHP XML-RPC extension installed.
2220

23-
A connection to the SoftLayer private network and a valid SoftLayer API username and key are required to call the SoftLayer API.
21+
A valid API username and key are required to call the SoftLayer API. A connection to the SoftLayer private network is required to connect to SoftLayer's private network API endpopints.
2422

2523
h2. Installation
2624

@@ -85,7 +83,7 @@ $update->entry = 'Hello!';
8583

8684
try {
8785
$update = $client->addUpdate($update);
88-
echo "Updated ticket 123456. The new update's id is " . $update->id . '.');
86+
echo "Updated ticket 123456. The new update's id is " . $update[0]->id . '.');
8987
} catch (Exception $e) {
9088
die('Unable to update ticket: ' . $e->getMessage());
9189
}

SoftLayer/SoapClient.class.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,27 @@ class Softlayer_SoapClient extends SoapClient
8181
const API_KEY = 'set me';
8282

8383
/**
84-
* The base URL of the SoftLayer SOAP API's WSDL files.
84+
* The base URL of the SoftLayer SOAP API's WSDL files over the public
85+
* Internet.
8586
*
8687
* @var string
8788
*/
88-
const API_BASE_URL = 'http://api.service.softlayer.com/soap/v3/';
89+
const API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/soap/v3/';
90+
91+
/**
92+
* The base URL of the SoftLayer SOAP API's WSDL files over SoftLayer's
93+
* private network.
94+
*
95+
* @var string
96+
*/
97+
const API_PRIVATE_ENDPOINT = 'http://api.service.softlayer.com/soap/v3/';
98+
99+
/**
100+
* The API endpoint base URL used by the client.
101+
*
102+
* @var string
103+
*/
104+
const API_BASE_URL = SoftLayer_SoapClient::API_PUBLIC_ENDPOINT;
89105

90106
/**
91107
* An optional SOAP timeout if you want to set a timeout independent of
@@ -110,6 +126,14 @@ class Softlayer_SoapClient extends SoapClient
110126
*/
111127
protected $_serviceName;
112128

129+
/**
130+
* The base URL to the SoftLayer API's WSDL files being used by this
131+
* client.
132+
*
133+
* @var string
134+
*/
135+
protected $_endpointUrl;
136+
113137
/**
114138
* Whether or not the current call is an asynchronous call.
115139
*
@@ -133,7 +157,6 @@ class Softlayer_SoapClient extends SoapClient
133157
*/
134158
public $asyncFunctionName = null;
135159

136-
137160
/**
138161
* If making an asynchronous call, then this is the result of an
139162
* asynchronous call as retuned from the
@@ -199,30 +222,49 @@ public function __call($functionName, $arguments = null)
199222
* @param int $id An optional object id if you're instantiating a particular SoftLayer API object. Setting an id defines this client's initialization parameter header.
200223
* @param string $username An optional API username if you wish to bypass SoftLayer_SoapClient's built-in username.
201224
* @param string $username An optional API key if you wish to bypass SoftLayer_SoapClient's built-in API key.
225+
* @param string $endpointUrl The API endpoint base URL you wish to connect to. Set this to SoftLayer_SoapClient::API_PRIVATE_ENDPOINT to connect via SoftLayer's private network.
202226
* @return SoftLayer_SoapClient
203227
*/
204-
public static function getClient($serviceName, $id = null, $username = null, $apiKey = null)
228+
public static function getClient($serviceName, $id = null, $username = null, $apiKey = null, $endpointUrl = null)
205229
{
206230
$serviceName = trim($serviceName);
207231

208232
if ($serviceName == null) {
209233
throw new Exception('Please provide a SoftLayer API service name.');
210234
}
211235

236+
/*
237+
* Default to use the public network API endpoint, otherwise use the
238+
* endpoint defined in API_PUBLIC_ENDPOINT, otherwise use the one
239+
* provided by the user.
240+
*/
241+
if (isset($endpointUrl)) {
242+
$endpointUrl = trim($endpointUrl);
243+
244+
if ($endpointUrl == null) {
245+
throw new Exception('Please provide a valid API endpoint.');
246+
}
247+
} elseif (self::API_BASE_URL != null) {
248+
$endpointUrl = self::API_BASE_URL;
249+
} else {
250+
$endpointUrl = SoftLayer_SoapClient::API_PUBLIC_ENDPOINT;
251+
}
252+
212253
if (is_null(self::SOAP_TIMEOUT)) {
213-
$soapClient = new SoftLayer_SoapClient(self::API_BASE_URL . $serviceName . '?wsdl');
254+
$soapClient = new SoftLayer_SoapClient($endpointUrl . $serviceName . '?wsdl');
214255
} else {
215-
$soapClient = new SoftLayer_SoapClient(self::API_BASE_URL . $serviceName . '?wsdl', array('connection_timeout' => self::SOAP_TIMEOUT));
256+
$soapClient = new SoftLayer_SoapClient($endpointUrl . $serviceName . '?wsdl', array('connection_timeout' => self::SOAP_TIMEOUT));
216257
}
217258

259+
$soapClient->_serviceName = $serviceName;
260+
$soapClient->_endpointUrl = $endpointUrl;
261+
218262
if ($username != null && $apiKey != null) {
219263
$soapClient->setAuthentication($username, $apiKey);
220264
} else {
221265
$soapClient->setAuthentication(self::API_USER, self::API_KEY);
222266
}
223267

224-
$soapClient->_serviceName = $serviceName;
225-
226268
if ($id != null) {
227269
$soapClient->setInitParameter($id);
228270
}
@@ -244,7 +286,7 @@ public static function getClient($serviceName, $id = null, $username = null, $ap
244286
*/
245287
public function addHeader($name, $value)
246288
{
247-
$this->_headers[$name] = new SoapHeader(self::API_BASE_URL, $name, $value);
289+
$this->_headers[$name] = new SoapHeader($this->_endpointUrl, $name, $value);
248290
}
249291

250292
/**

SoftLayer/SoapClient/AsynchronousAction.class.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,15 @@ public function __construct($soapClient, $functionName, $request, $location, $ac
169169
'Connection: close',
170170
);
171171

172+
if ($protocol == 'https') {
173+
$host = 'ssl://' . $host;
174+
$port = 443;
175+
} else {
176+
$port = 80;
177+
}
178+
172179
$data = implode("\r\n", $headers) . "\r\n\r\n" . $request . "\r\n";
173-
$this->_socket = fsockopen($host, 80, $errorNumber, $errorMessage);
180+
$this->_socket = fsockopen($host, $port, $errorNumber, $errorMessage);
174181

175182
if ($this->_socket === false) {
176183
$this->_socket = null;

SoftLayer/XmlrpcClient.class.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,25 @@ class Softlayer_XmlrpcClient
7474
const API_KEY = 'set me';
7575

7676
/**
77-
* The base URL of SoftLayer XML-RPC API's endpoints.
77+
* The base URL of SoftLayer XML-RPC API's public network endpoints.
7878
*
7979
* @var string
8080
*/
81-
const API_BASE_URL = 'http://api.service.softlayer.com/xmlrpc/v3/';
81+
const API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/xmlrpc/v3/';
82+
83+
/**
84+
* The base URL of SoftLayer XML-RPC API's private network endpoints.
85+
*
86+
* @var string
87+
*/
88+
const API_PRIVATE_ENDPOINT = 'http://api.service.softlayer.com/xmlrpc/v3/';
89+
90+
/**
91+
* The API endpoint base URL used by the client.
92+
*
93+
* @var string
94+
*/
95+
const API_BASE_URL = SoftLayer_XmlrpcClient::API_PUBLIC_ENDPOINT;
8296

8397
/**
8498
* The headers to send along with a SoftLayer API call
@@ -95,6 +109,13 @@ class Softlayer_XmlrpcClient
95109
*/
96110
protected $_serviceName;
97111

112+
/**
113+
* The base URL of SoftLayer XML-RPC API's endpoints used by this client.
114+
*
115+
* @var string
116+
*/
117+
protected $_endpointUrl;
118+
98119
/**
99120
* Execute a SoftLayer API method
100121
*
@@ -119,10 +140,10 @@ public function __call($functionName, $arguments = null)
119140
'content' => $encodedRequest
120141
)));
121142

122-
$file = file_get_contents(self::API_BASE_URL . $this->_serviceName, false, $context);
143+
$file = file_get_contents($this->_endpointUrl . $this->_serviceName, false, $context);
123144

124145
if ($file === false) {
125-
throw new Exception('Unable to contact the SoftLayer API at ' . self::API_BASE_URL . $serviceName . '.');
146+
throw new Exception('Unable to contact the SoftLayer API at ' . $this->_endpointUrl . $serviceName . '.');
126147
}
127148

128149
$result = xmlrpc_decode($file);
@@ -152,9 +173,10 @@ public function __call($functionName, $arguments = null)
152173
* @param int $id An optional object id if you're instantiating a particular SoftLayer API object. Setting an id defines this client's initialization parameter header.
153174
* @param string $username An optional API username if you wish to bypass SoftLayer_XmlrpcClient's built-in username.
154175
* @param string $username An optional API key if you wish to bypass SoftLayer_XmlrpcClient's built-in API key.
176+
* @param string $endpointUrl The API endpoint base URL you wish to connect to. Set this to SoftLayer_XmlrpcClient::API_PRIVATE_ENDPOINT to connect via SoftLayer's private network.
155177
* @return SoftLayer_XmlrpcClient
156178
*/
157-
public static function getClient($serviceName, $id = null, $username = null, $apiKey = null)
179+
public static function getClient($serviceName, $id = null, $username = null, $apiKey = null, $endpointUrl = null)
158180
{
159181
$serviceName = trim($serviceName);
160182
$id = trim($id);
@@ -167,6 +189,25 @@ public static function getClient($serviceName, $id = null, $username = null, $ap
167189

168190
$client = new Softlayer_XmlrpcClient();
169191

192+
/*
193+
* Default to use the public network API endpoint, otherwise use the
194+
* endpoint defined in API_PUBLIC_ENDPOINT, otherwise use the one
195+
* provided by the user.
196+
*/
197+
if (isset($endpointUrl)) {
198+
$endpointUrl = trim($endpointUrl);
199+
200+
if ($endpointUrl == null) {
201+
throw new Exception('Please provide a valid API endpoint.');
202+
}
203+
204+
$client->_endpointUrl = $endpointUrl;
205+
} elseif (self::API_BASE_URL != null) {
206+
$client->_endpointUrl = self::API_BASE_URL;
207+
} else {
208+
$client->_endpointUrl = SoftLayer_XmlrpcClient::API_PUBLIC_ENDPOINT;
209+
}
210+
170211
if ($username != null && $apiKey != null) {
171212
$client->setAuthentication($username, $apiKey);
172213
} else {

0 commit comments

Comments
 (0)