Skip to content

Commit 8afd99b

Browse files
authoredNov 7, 2022
Merge pull request #29 from softlayer/php8.1.12
Php8.1.12 support
2 parents dfe76dd + 3d36916 commit 8afd99b

File tree

11 files changed

+2379
-1036
lines changed

11 files changed

+2379
-1036
lines changed
 

‎.github/workflows/test.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build-test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: php-actions/composer@v6 # or alternative dependency management
12+
with:
13+
php_version: '8.1'
14+
php_extensions: 'soap'
15+
- uses: php-actions/phpunit@v3
16+
with:
17+
php_version: '8.1'
18+
php_extensions: 'soap'
19+
- uses: actions/upload-artifact@v2
20+
with:
21+
name: debug-output
22+
path: output.log

‎.travis.yml

-30
This file was deleted.

‎README.md

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
# A SoftLayer API PHP client.
22

3-
[![Build Status](https://travis-ci.org/softlayer/softlayer-api-php-client.svg?branch=master)](https://travis-ci.org/softlayer/softlayer-api-php-client)
3+
![Build Status](https://github.com/softlayer/softlayer-api-php-client/actions/workflows/test.yml/badge.svg)
44

55
## Warning
66

7-
```
8-
The latest version 1.x is not backwards-compatible.
9-
It is necessary to update scripts to function properly with the new version.
10-
```
7+
Use [v1.2.0](https://github.com/softlayer/softlayer-api-php-client/releases/tag/v1.2) for older PHP versions (php < 8.0) . [v2.0.0](https://github.com/softlayer/softlayer-api-php-client/releases/tag/v2.0.0) for php 8.0 or higher.
8+
9+
[PHP 8.0 removed XMLRPC](https://php.watch/versions/8.0/xmlrpc) as a built in extension. As such, it is no longer required as part of the composer file in this project. The XmlRpcClient still exists here if you need it, but we assume most users are using the SoapClient. If there are any issues with this [Let us know on github](https://github.com/softlayer/softlayer-api-php-client/issues)
1110

1211
## Overview
1312

1413
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.
1514

16-
Making API calls using the `\SoftLayer\SoapClient` or `\SoftLayer\XmlRpcClient` classes is done in the following steps:
15+
Making API calls using the `\SoftLayer\SoapClient` is done in the following steps:
1716

18-
1. 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.
19-
2. Define and add optional headers to the client, such as object masks and result limits.
20-
3. 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.
17+
1. Instantiate a new `\SoftLayer\SoapClient` object using the `\SoftLayer\SoapClient::getClient()` method. 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.
18+
2. Use `\SoftLayer\SoapClient::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.
19+
3. Define and add optional headers to the client, such as object masks and result limits.
20+
4. 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.
2121

2222
Once your method is executed you may continue using the same client if you need to connect to the same service or define another client object if you wish to work with multiple services at once.
2323

24-
The most up to date version of this library can be found on the SoftLayer github public repositories: [http://github.com/softlayer/](http://github.com/softlayer/) . Please post to the SoftLayer forums [Stack Overflow](https://stackoverflow.com) or open a support ticket in the SoftLayer customer portal if you have any questions regarding use of this library. If you use Stack Overflow please tag your posts with “SoftLayer” so our team can easily find your post.
24+
25+
The most up to date version of this library can be found on the SoftLayer github public repositories: [https://github.com/softlayer/softlayer-api-php-client](https://github.com/softlayer/softlayer-api-php-client) . Any issues using this library, please open a [Github Issue](https://github.com/softlayer/softlayer-api-php-client/issues)
26+
2527

2628
## System Requirements
2729

28-
The `\SoftLayer\SoapClient` class requires at least PHP 5.3.0 and the PHP SOAP enxtension installed. The `\SoftLayer\XmlRpcClient` class requires PHP at least PHP 5 and the PHP XML-RPC extension installed.
30+
The `\SoftLayer\SoapClient` class requires at least PHP 8.0.0 and the PHP SOAP enxtension installed and enabled (`extension=soap` in the php.ini file).
31+
Since [php 8.0 has removed xmlrpc extension](https://php.watch/versions/8.0/xmlrpc) you will need to manually install this library to use the `\SoftLayer\XmlRpcClient`. If you are using an earlier version of php that still includes ext-xml, please use v1.2.0 of this library.
2932

30-
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.
33+
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. See [Authenticating to the SoftLayer API](https://sldn.softlayer.com/article/authenticating-softlayer-api/) for how to get these API keys.
3134

3235
## Installation
3336

3437
Install the SoftLayer API client using [Composer](https://getcomposer.org/).
3538
```bash
36-
composer require softlayer/softlayer-api-php-client:~1.0@dev
39+
composer require softlayer/softlayer-api-php-client:~2.0.0
3740
```
3841

3942
## Usage
@@ -107,4 +110,4 @@ This software is written by the SoftLayer Development Team <[sldn@softlayer.com]
107110

108111
## Copyright
109112

110-
This software is Copyright &copy; 2009 – 2010 [SoftLayer Technologies, Inc](http://www.softlayer.com/). See the bundled LICENSE.textile file for more information.
113+
This software is Copyright &copy; 2009 – 2022 [SoftLayer Technologies, Inc](http://www.softlayer.com/). See the bundled LICENSE.textile file for more information.

‎composer.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "softlayer/softlayer-api-php-client",
33
"description": "SoftLayer API PHP client",
44
"keywords": ["softlayer"],
5-
"homepage": "http://sldn.softlayer.com/article/PHP",
5+
"homepage": "https://sldn.softlayer.com/php/",
66
"license": "MIT",
77
"authors": [
88
{
@@ -11,8 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^5.3|^7.0",
15-
"ext-xmlrpc": "*",
14+
"php": ">=8.0",
1615
"ext-soap": "*"
1716
},
1817
"autoload": {
@@ -26,15 +25,12 @@
2625
}
2726
},
2827
"extra": {
29-
"branch-alias": {
30-
"dev-master": "1.0-dev"
31-
},
3228
"config": {
3329
"sort-packages": true
3430
}
3531
},
3632
"require-dev": {
37-
"friendsofphp/php-cs-fixer": "^1.13|^2.0",
38-
"phpunit/phpunit": "^5.4.3|^6.0|^7.0"
33+
"friendsofphp/php-cs-fixer": ">=3.13.0",
34+
"phpunit/phpunit": ">=9.0"
3935
}
4036
}

‎composer.lock

+1,982-845
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎example.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2022, 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+
use SoftLayer\Common\ObjectMask;
31+
use SoftLayer\SoapClient;
32+
use SoftLayer\XmlRpcClient;
33+
34+
/**
35+
* Start by including the autoload class.
36+
*
37+
* If you wish to use the XML-RPC API then replace mentions of
38+
* SoapClient with XmlRpcClient.
39+
*/
40+
$files = [
41+
__DIR__.'/vendor/autoload.php',
42+
__DIR__.'/../../autoload.php',
43+
];
44+
45+
$autoload = false;
46+
foreach ($files as $file) {
47+
if (is_file($file)) {
48+
$autoload = include_once $file;
49+
50+
break;
51+
}
52+
}
53+
54+
if (!$autoload) {
55+
die(<<<MSG
56+
Unable to find autoload.php file, please use composer to load dependencies:
57+
58+
wget http://getcomposer.org/composer.phar
59+
php composer.phar install
60+
61+
Visit http://getcomposer.org/ for more information.
62+
63+
MSG
64+
);
65+
}
66+
67+
/**
68+
* It's possible to define your SoftLayer API username and key directly in the
69+
* class file, but it's far easier to define them before creating your API client.
70+
*/
71+
$apiUsername = getenv('SL_USER');
72+
$apiKey = getenv('SL_APIKEY');
73+
74+
/**
75+
* Usage:
76+
* SoapClient::getClient([API Service], <object id>, [username], [API key]);
77+
*
78+
* API Service: The name of the API service you wish to connect to.
79+
* id: An optional id to initialize your API service with, if you're
80+
* interacting with a specific object. If you don't need to specify
81+
* an id then pass null to the client.
82+
* username: Your SoftLayer API username.
83+
* API key: Your SoftLayer API key,
84+
*/
85+
$client = SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
86+
$objectMask = "mask[id,companyName]";
87+
$client->setObjectMask($objectMask);
88+
89+
/**
90+
* Once your client object is created you can call API methods for that service
91+
* directly against your client object. A call may throw an exception on error,
92+
* so it's best to try your call and catch exceptions.
93+
*
94+
* This example calls the getObject() method in the SoftLayer_Account API
95+
* service. <http://sldn.softlayer.com/reference/services/SoftLayer_Account/getObject>
96+
* It retrieves basic account information, and is a great way to test your API
97+
* account and connectivity.
98+
*/
99+
100+
try {
101+
$result = $client->getObject();
102+
//$client->__getLastRequest();
103+
print_r($result);
104+
} catch (\Exception $e) {
105+
die($e->getMessage());
106+
}
107+
108+
/**
109+
* In this example we will get all of the VirtualGuests on our account. And for each guest we will print out
110+
* some basic information about them, along with make another API call to get its primaryIpAddress.
111+
*/
112+
113+
// Declare an API client to connect to the SoftLayer_Ticket API service.
114+
$client = SoapClient::getClient('SoftLayer_Account', null, $apiUsername, $apiKey);
115+
116+
117+
// Assign an object mask to our API client:
118+
$objectMask = "mask[id, hostname, datacenter[longName]]";
119+
$client->setObjectMask($objectMask);
120+
121+
try {
122+
$virtualGuests = $client->getVirtualGuests();
123+
print("Id, Hostname, Datacenter, Ip Address\n");
124+
foreach ($virtualGuests as $guest) {
125+
$guestClient = SoapClient::getClient('SoftLayer_Virtual_Guest', $guest->id, $apiUsername, $apiKey);
126+
$ipAddress = $guestClient->getPrimaryIpAddress();
127+
print($guest->id . ", " . $guest->hostname . ", " . $guest->datacenter->longName . ", " . $ipAddress . "\n");
128+
break;
129+
}
130+
} catch (\Exception $e) {
131+
die('Unable to retrieve virtual guests: ' . $e->getMessage());
132+
}

‎phpunit.xml.dist

+14-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="false"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
bootstrap="vendor/autoload.php"
12-
>
13-
<testsuites>
14-
<testsuite name="SoftLayer PHP Client test suite">
15-
<directory suffix="Test.php">./tests</directory>
16-
</testsuite>
17-
</testsuites>
18-
19-
<filter>
20-
<whitelist>
21-
<directory>./</directory>
22-
<exclude>
23-
<directory>./tests</directory>
24-
</exclude>
25-
</whitelist>
26-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="false" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory>./</directory>
6+
</include>
7+
<exclude>
8+
<directory>./tests</directory>
9+
</exclude>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="SoftLayer PHP Client test suite">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
2716
</phpunit>

‎src/Common/ObjectMask.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,13 @@
5757
* making your SoftLayer API calls.
5858
*
5959
* For more on object mask usage in the SoftLayer API please see
60-
* http://sldn.softlayer.com/article/Using_Object_Masks_in_the_SoftLayer_API.
60+
* https://sldn.softlayer.com/article/object-masks/ .
6161
*
6262
* The most up to date version of this library can be found on the SoftLayer
63-
* github public repositories: http://github.com/softlayer/ . Please post to
64-
* the SoftLayer forums <http://forums.softlayer.com/> or open a support ticket
65-
* in the SoftLayer customer portal if you have any questions regarding use of
66-
* this library.
63+
* github public repositories: http://github.com/softlayer/ .
6764
*
6865
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
69-
* @copyright Copyright (c) 2009 - 2010, Softlayer Technologies, Inc
66+
* @copyright Copyright (c) 2009 - 2022, Softlayer Technologies, Inc
7067
* @license http://sldn.softlayer.com/article/License
7168
*
7269
* @see SoapClient::setObjectMask()

‎src/SoapClient.php

+86-67
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
2-
3-
/*
4-
* Copyright (c) 2009 - 2010, SoftLayer Technologies, Inc. All rights reserved.
2+
/**
3+
* Copyright (c) 2009 - 2022, SoftLayer Technologies, Inc. All rights reserved.
54
*
65
* Redistribution and use in source and binary forms, with or without
76
* modification, are permitted provided that the following conditions are met:
@@ -36,69 +35,60 @@
3635
/**
3736
* A SoftLayer API SOAP Client.
3837
*
39-
* Please see the bundled README.textile file for more details and usage
40-
* information.
38+
* Please see the bundled README file for more details and usage information.
4139
*
42-
* This client supports sending multiple calls in parallel to the SoftLayer
43-
* API. Please see the documentation in the
44-
* AsynchronousAction class in
40+
* This client supports sending multiple calls in parallel to the SoftLaye API.
41+
* Please see the documentation in the AsynchronousAction class in
4542
* SoapClient/AsynchronousAction.php for details.
4643
*
4744
* The most up to date version of this library can be found on the SoftLayer
48-
* github public repositories: http://github.com/softlayer/ . Please post to
49-
* the SoftLayer forums <http://forums.softlayer.com/> or open a support ticket
50-
* in the SoftLayer customer portal if you have any questions regarding use of
51-
* this library.
45+
* github public repositories: https://github.com/softlayer/softlayer-api-php-client .
46+
* For any issues with this library, please open a github issue
5247
*
53-
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
54-
* @copyright Copyright (c) 2009 - 2010, Softlayer Technologies, Inc
55-
* @license http://sldn.softlayer.com/article/License
56-
*
57-
* @see http://sldn.softlayer.com/article/The_SoftLayer_API The SoftLayer API
58-
* @see AsynchronousAction
48+
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
49+
* @copyright Copyright (c) 2009 - 2022, Softlayer Technologies, Inc
50+
* @license http://sldn.softlayer.com/article/License
51+
* @link https://sldn.softlayer.com/article/php/ The SoftLayer API
52+
* @see AsynchronousAction
53+
5954
*/
6055
class SoapClient extends \SoapClient
6156
{
6257
/**
63-
* Your SoftLayer API username. You may overide this value when calling
64-
* getClient().
58+
* Your SoftLayer API username. You may overide this value when calling getClient().
6559
*
6660
* @var string
6761
*/
6862
const API_USER = 'set me';
6963

7064
/**
71-
* Your SoftLayer API user's authentication key. You may overide this value
72-
* when calling getClient().
73-
*
74-
* @see https://manage.softlayer.com/Administrative/apiKeychain API key management in the SoftLayer customer portal
65+
* Your SoftLayer API user's authentication key. You may overide this value when calling getClient().
7566
*
67+
* @link https://sldn.softlayer.com/article/authenticating-softlayer-api/ API key management in the SoftLayer customer portal
7668
* @var string
7769
*/
7870
const API_KEY = 'set me';
7971

8072
/**
81-
* The base URL of the SoftLayer SOAP API's WSDL files over the public
82-
* Internet.
73+
* The base URL of the SoftLayer SOAP API's WSDL files over the public Internet.
8374
*
8475
* @var string
8576
*/
86-
const API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/soap/v3/';
77+
const API_PUBLIC_ENDPOINT = 'https://api.softlayer.com/soap/v3.1/';
8778

8879
/**
89-
* The base URL of the SoftLayer SOAP API's WSDL files over SoftLayer's
90-
* private network.
80+
* The base URL of the SoftLayer SOAP API's WSDL files over SoftLayer's private network.
9181
*
9282
* @var string
9383
*/
94-
const API_PRIVATE_ENDPOINT = 'http://api.service.softlayer.com/soap/v3/';
84+
const API_PRIVATE_ENDPOINT = 'http://api.service.softlayer.com/soap/v3.1/';
9585

9686
/**
9787
* The namespace to use for calls to the API.
9888
*
9989
* $var string
10090
*/
101-
const DEFAULT_NAMESPACE = 'http://api.service.softlayer.com/soap/v3/';
91+
const DEFAULT_NAMESPACE = 'http://api.service.softlayer.com/soap/v3.1/';
10292

10393
/**
10494
* The API endpoint base URL used by the client.
@@ -108,8 +98,7 @@ class SoapClient extends \SoapClient
10898
const API_BASE_URL = SoapClient::API_PUBLIC_ENDPOINT;
10999

110100
/**
111-
* An optional SOAP timeout if you want to set a timeout independent of
112-
* PHP's socket timeout.
101+
* An optional SOAP timeout if you want to set a timeout independent of PHP's socket timeout.
113102
*
114103
* @var int
115104
*/
@@ -178,6 +167,7 @@ class SoapClient extends \SoapClient
178167
*/
179168
public $oneWay;
180169

170+
181171
/**
182172
* Execute a SoftLayer API method.
183173
*
@@ -187,7 +177,7 @@ public function __call($functionName, $arguments = null)
187177
{
188178
// Determine if we shoud be making an asynchronous call. If so strip
189179
// "Async" from the end of the method name.
190-
if (null === $this->_asyncResult) {
180+
if ($this->_asyncResult === null) {
191181
$this->_asynchronous = false;
192182
$this->_asyncAction = null;
193183

@@ -200,7 +190,7 @@ public function __call($functionName, $arguments = null)
200190
}
201191

202192
try {
203-
$result = parent::__call($functionName, $arguments, null, $this->_headers, null);
193+
$result = parent::__soapCall($functionName, $arguments, null, $this->_headers);
204194
} catch (\SoapFault $e) {
205195
throw new \Exception('There was an error querying the SoftLayer API: ' . $e->getMessage(), 0, $e);
206196
}
@@ -218,24 +208,23 @@ public function __call($functionName, $arguments = null)
218208
/**
219209
* Create a SoftLayer API SOAP Client.
220210
*
221-
* Retrieve a new SoapClient object for a specific SoftLayer API
222-
* service using either the class' constants API_USER and API_KEY or a
223-
* custom username and API key for authentication. Provide an optional id
224-
* value if you wish to instantiate a particular SoftLayer API object.
211+
* Retrieve a new SoapClient object for a specific SoftLayer API service using either the class'
212+
* constants API_USER and API_KEY or a custom username and API key for authentication.
213+
* Provide an optional id value if you wish to instantiate a particular SoftLayer API object.
225214
*
226215
* @param string $serviceName The name of the SoftLayer API service you wish to query
227216
* @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.
228217
* @param string $username an optional API username if you wish to bypass SoapClient's built-in username
229218
* @param string $apiKey an optional API key if you wish to bypass SoapClient's built-in API key
230219
* @param string $endpointUrl The API endpoint base URL you wish to connect to. Set this to SoapClient::API_PRIVATE_ENDPOINT to connect via SoftLayer's private network.
231-
*
220+
* @param bool $trace Enabled SOAP trace in the client object.
232221
* @return SoapClient
233222
*/
234-
public static function getClient($serviceName, $id = null, $username = null, $apiKey = null, $endpointUrl = null)
223+
public static function getClient($serviceName, $id = null, $username = null, $apiKey = null, $endpointUrl = null, $trace = false)
235224
{
236225
$serviceName = trim($serviceName);
237226

238-
if ('' === $serviceName) {
227+
if (empty($serviceName)) {
239228
throw new \Exception('Please provide a SoftLayer API service name.');
240229
}
241230

@@ -244,34 +233,36 @@ public static function getClient($serviceName, $id = null, $username = null, $ap
244233
* endpoint defined in API_PUBLIC_ENDPOINT, otherwise use the one
245234
* provided by the user.
246235
*/
247-
if (null !== $endpointUrl) {
236+
if (!empty($endpointUrl)) {
248237
$endpointUrl = trim($endpointUrl);
249238

250-
if ('' === $endpointUrl) {
239+
if (empty($endpointUrl)) {
251240
throw new \Exception('Please provide a valid API endpoint.');
252241
}
253-
} elseif (null !== self::API_BASE_URL) {
242+
} elseif (self::API_BASE_URL !== null) {
254243
$endpointUrl = self::API_BASE_URL;
255244
} else {
256245
$endpointUrl = self::API_PUBLIC_ENDPOINT;
257246
}
258247

259-
if (null === self::SOAP_TIMEOUT) {
260-
$soapClient = new self($endpointUrl.$serviceName.'?wsdl');
261-
} else {
262-
$soapClient = new self($endpointUrl.$serviceName.'?wsdl', array('connection_timeout' => self::SOAP_TIMEOUT));
248+
$soapOptions = ['trace' => $trace];
249+
if (is_null(self::SOAP_TIMEOUT) == false) {
250+
$soapOptions['connection_timeout'] = self::SOAP_TIMEOUT;
263251
}
264252

253+
$soapClient = new self($endpointUrl . $serviceName . '?wsdl', $soapOptions);
254+
255+
265256
$soapClient->_serviceName = $serviceName;
266257
$soapClient->_endpointUrl = $endpointUrl;
267258

268-
if (null !== $username && null !== $apiKey) {
259+
if (!empty($username) && !empty($apiKey)) {
269260
$soapClient->setAuthentication($username, $apiKey);
270261
} else {
271262
$soapClient->setAuthentication(self::API_USER, self::API_KEY);
272263
}
273264

274-
if (null !== $id) {
265+
if (!empty($id)) {
275266
$soapClient->setInitParameter($id);
276267
}
277268

@@ -324,7 +315,7 @@ public function removeHeader($name)
324315
* Use this method if you wish to bypass the API_USER and API_KEY class
325316
* constants and set custom authentication per API call.
326317
*
327-
* @see https://manage.softlayer.com/Administrative/apiKeychain API key management in the SoftLayer customer portal
318+
* @see https://sldn.softlayer.com/article/authenticating-softlayer-api/ API key management in the SoftLayer customer portal
328319
*
329320
* @param string $username
330321
* @param string $apiKey
@@ -335,13 +326,13 @@ public function setAuthentication($username, $apiKey)
335326
{
336327
$username = trim($username);
337328

338-
if ('' === $username) {
329+
if (empty($username)) {
339330
throw new \Exception('Please provide a SoftLayer API username.');
340331
}
341332

342333
$apiKey = trim($apiKey);
343334

344-
if ('' === $apiKey) {
335+
if (empty($apiKey)) {
345336
throw new \Exception('Please provide a SoftLayer API key.');
346337
}
347338

@@ -363,17 +354,16 @@ public function setAuthentication($username, $apiKey)
363354
* 1234 in the SoftLayer_Hardware_Server Service instructs the API to act on
364355
* server record 1234 in your method calls.
365356
*
366-
* @see http://sldn.softlayer.com/article/Using_Initialization_Parameters_in_the_SoftLayer_API Using Initialization Parameters in the SoftLayer API
367-
*
368-
* @param int $id the ID number of the SoftLayer API object you wish to instantiate
369-
*
357+
* @link https://sldn.softlayer.com/article/using-initialization-parameters-softlayer-api/ Using Initialization Parameters in the SoftLayer API
358+
* @param int $id The ID number of the SoftLayer API object you wish to instantiate.
359+
370360
* @return SoapClient
371361
*/
372362
public function setInitParameter($id)
373363
{
374364
$id = trim($id);
375365

376-
if ('' !== $id) {
366+
if ($id !== '') {
377367
$initParameters = new \stdClass();
378368
$initParameters->id = $id;
379369
$this->addHeader($this->_serviceName.'InitParameters', $initParameters);
@@ -398,7 +388,7 @@ public function setInitParameter($id)
398388
*/
399389
public function setObjectMask($mask)
400390
{
401-
if (null !== $mask) {
391+
if (!empty($mask)) {
402392
$header = 'SoftLayer_ObjectMask';
403393

404394
if ($mask instanceof ObjectMask) {
@@ -427,7 +417,7 @@ public function setObjectMask($mask)
427417
*/
428418
public function setObjectFilter($objectFilter)
429419
{
430-
if (null !== $objectFilter) {
420+
if (!empty($objectFilter)) {
431421
$header = sprintf('%sObjectFilter', $this->_serviceName);
432422
$this->addHeader($header, $objectFilter);
433423
}
@@ -442,11 +432,10 @@ public function setObjectFilter($objectFilter)
442432
* support a way to limit the number of results retrieved from the SoftLayer
443433
* API in a way akin to an SQL LIMIT statement.
444434
*
445-
* @see http://sldn.softlayer.com/article/Using_Result_Limits_in_the_SoftLayer_API Using Result Limits in the SoftLayer API
446-
*
447-
* @param int $limit the number of results to limit your SoftLayer API call to
448-
* @param int $offset an optional offset to begin your SoftLayer API call's returned result set at
449-
*
435+
* @link https://sldn.softlayer.com/article/using-result-limits-softlayer-api/ Using Result Limits in the SoftLayer API
436+
* @param int $limit The number of results to limit your SoftLayer API call to.
437+
* @param int $offset An optional offset to begin your SoftLayer API call's returned result set at.
438+
450439
* @return SoapClient
451440
*/
452441
public function setResultLimit($limit, $offset = 0)
@@ -476,7 +465,7 @@ public function setResultLimit($limit, $offset = 0)
476465
public function __doRequest($request, $location, $action, $version, $one_way = false)
477466
{
478467
// Don't make a call if we already have an asynchronous result.
479-
if (null !== $this->_asyncResult) {
468+
if ($this->_asyncResult !== null) {
480469
$result = $this->_asyncResult;
481470
$this->_asyncResult = null;
482471

@@ -520,4 +509,34 @@ public function handleAsyncResult($functionName, $result)
520509

521510
return $this->__call($functionName, array());
522511
}
512+
513+
/**
514+
* Returns the headers set for this client object.
515+
*
516+
* @return array
517+
*/
518+
public function getHeaders()
519+
{
520+
return $this->_headers;
521+
}
522+
523+
/**
524+
* Returns the service name
525+
*
526+
* @return string
527+
*/
528+
public function getServiceName()
529+
{
530+
return $this->_serviceName;
531+
}
532+
533+
/**
534+
* Returns the endpoint URL
535+
*
536+
* @return string
537+
*/
538+
public function getEndpointUrl()
539+
{
540+
return $this->_endpointUrl;
541+
}
523542
}

‎src/SoapClient/AsynchronousAction.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class AsynchronousAction
141141
protected $_socket;
142142

143143
/**
144-
* Perform an asynchronous SoftLayer SOAP call.
144+
* Perform an asynchronous SoftLayer SOAP call
145145
*
146146
* Create a raw socket connection to the URL specified by the
147147
* SoapClient class and send SOAP HTTP headers and request XML to
@@ -175,7 +175,7 @@ public function __construct($soapClient, $functionName, $request, $location, $ac
175175
'Connection: close',
176176
);
177177

178-
if ('https' === $protocol) {
178+
if ($protocol === 'https') {
179179
$host = 'ssl://'.$host;
180180
$port = 443;
181181
} else {
@@ -185,12 +185,12 @@ public function __construct($soapClient, $functionName, $request, $location, $ac
185185
$data = implode("\r\n", $headers)."\r\n\r\n".$request."\r\n";
186186
$this->_socket = fsockopen($host, $port, $errorNumber, $errorMessage);
187187

188-
if (false === $this->_socket) {
188+
if ($this->_socket === false) {
189189
$this->_socket = null;
190190
throw new \Exception('Unable to make an asynchronous SoftLayer API call: '.$errorNumber.': '.$errorMessage);
191191
}
192192

193-
if (false === fwrite($this->_socket, $data)) {
193+
if (fwrite($this->_socket, $data) === false) {
194194
throw new \Exception('Unable to write data to an asynchronous SoftLayer API call.');
195195
}
196196
}

‎tests/SoapClientTest.php

+115-37
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,129 @@
11
<?php
22

3-
/*
4-
* Copyright (c) 2009 - 2010, SoftLayer Technologies, Inc. All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
7-
* modification, are permitted provided that the following conditions are met:
8-
*
9-
* * Redistributions of source code must retain the above copyright notice,
10-
* this list of conditions and the following disclaimer.
11-
* * Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
13-
* and/or other materials provided with the distribution.
14-
* * Neither SoftLayer Technologies, Inc. nor the names of its contributors may
15-
* be used to endorse or promote products derived from this software without
16-
* specific prior written permission.
17-
*
18-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21-
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22-
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23-
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24-
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25-
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26-
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27-
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28-
* POSSIBILITY OF SUCH DAMAGE.
29-
*/
30-
313
namespace SoftLayer\Tests;
324

335
use PHPUnit\Framework\TestCase;
346
use SoftLayer\SoapClient;
357

36-
/**
37-
* @author Javier Spagnoletti <phansys@gmail.com>
38-
*/
398
class SoapClientTest extends TestCase
409
{
41-
/**
42-
* @expectedException \Exception
43-
* @expectedExceptionMessage There was an error querying the SoftLayer API: Invalid API token.
44-
*/
45-
public function testSoapClientException()
10+
11+
public function testGetClient()
12+
{
13+
$client = SoapClient::getClient('SoftLayer_Ticket', 123456, 'apiUsername', 'apiKey');
14+
$headers = $client->getHeaders();
15+
$this->assertEquals('apiUsername', $headers['authenticate']->data->username);
16+
$this->assertEquals('apiKey', $headers['authenticate']->data->apiKey);
17+
$this->assertEquals(123456, $headers['SoftLayer_TicketInitParameters']->data->id);
18+
}
19+
20+
public function testGetClientDefaults()
21+
{
22+
$client = SoapClient::getClient('SoftLayer_Ticket');
23+
$headers = $client->getHeaders();
24+
$this->assertEquals('set me', $headers['authenticate']->data->username);
25+
$this->assertEquals('set me', $headers['authenticate']->data->apiKey);
26+
$this->assertFalse(array_key_exists('SoftLayer_TicketInitParameters', $headers));
27+
}
28+
29+
public function testGetClientNoService()
30+
{
31+
$this->expectException(\Exception::class);
32+
$this->expectExceptionMessage('Please provide a SoftLayer API service name.');
33+
$client = SoapClient::getClient('', 123456, 'apiUsername', 'apiKey');
34+
}
35+
36+
public function testGetClientNoEndpoint()
37+
{
38+
$this->expectException(\Exception::class);
39+
$this->expectExceptionMessage('Please provide a valid API endpoint.');
40+
$client = SoapClient::getClient('SoftLayer_Account', 123456, 'apiUsername', 'apiKey', $endpointUrl=' ');
41+
}
42+
43+
public function testSetObjectMask()
44+
{
45+
$client = SoapClient::getClient('SoftLayer_Ticket', 123456, 'apiUsername', 'apiKey');
46+
$mask = "mask[id,test]";
47+
$client->setObjectMask($mask);
48+
$headers = $client->getHeaders();
49+
$this->assertEquals($mask, $headers['SoftLayer_ObjectMask']->data->mask);
50+
}
51+
52+
public function testSetObjectMaskClass()
53+
{
54+
$client = SoapClient::getClient('SoftLayer_Ticket', 123456, 'apiUsername', 'apiKey');
55+
$mask = new \SoftLayer\Common\ObjectMask();
56+
$mask->id;
57+
$mask->username;
58+
$client->setObjectMask($mask);
59+
$headers = $client->getHeaders();
60+
61+
$this->assertEquals($mask, $headers['SoftLayer_TicketObjectMask']->data->mask);
62+
}
63+
64+
public function testSetObjecFilter()
4665
{
4766
$client = SoapClient::getClient('SoftLayer_Ticket', 123456, 'apiUsername', 'apiKey');
67+
$filter = new \stdClass();
68+
$filter->test1 = new \stdClass();
69+
$filter->test1->operation = "testFilter";
70+
$client->setObjectFilter($filter);
71+
$headers = $client->getHeaders();
72+
$this->assertEquals("testFilter", $headers['SoftLayer_TicketObjectFilter']->data->test1->operation);
73+
}
74+
75+
public function testSetAuthentication()
76+
{
77+
$client = SoapClient::getClient('SoftLayer_Ticket');
78+
$headers = $client->getHeaders();
79+
$this->assertEquals('set me', $headers['authenticate']->data->username);
80+
$this->assertEquals('set me', $headers['authenticate']->data->apiKey);
81+
82+
$this->expectException(\Exception::class);
83+
$this->expectExceptionMessage('Please provide a SoftLayer API key.');
84+
$client->setAuthentication('username1', '');
85+
86+
$this->expectException(\Exception::class);
87+
$this->expectExceptionMessage('Please provide a SoftLayer API username.');
88+
$client->setAuthentication(null, 'apikey2');
89+
90+
$client->setAuthentication('username1', 'apikey2');
91+
$this->assertEquals('username1', $headers['authenticate']->data->username);
92+
$this->assertEquals('apikey2', $headers['authenticate']->data->apiKey);
93+
}
94+
95+
public function testRemoveHeader()
96+
{
97+
$client = SoapClient::getClient('SoftLayer_Ticket');
98+
$headers = $client->getHeaders();
99+
$this->assertTrue(array_key_exists('authenticate', $headers));
100+
$client->removeHeader('authenticate');
101+
$headers = $client->getHeaders();
102+
$this->assertFalse(array_key_exists('authenticate', $headers));
103+
}
104+
105+
public function testSetInitParameters()
106+
{
107+
$client = SoapClient::getClient('SoftLayer_Ticket');
108+
$headers = $client->getHeaders();
109+
$this->assertFalse(array_key_exists('SoftLayer_TicketInitParameters', $headers));;
110+
$client->setInitParameter(999);
111+
$headers = $client->getHeaders();
112+
$this->assertTrue(array_key_exists('authenticate', $headers));
113+
$this->assertEquals(999, $headers['SoftLayer_TicketInitParameters']->data->id);
114+
}
48115

49-
$client->getObject();
116+
public function testSetResultLimit()
117+
{
118+
$client = SoapClient::getClient('SoftLayer_Ticket');
119+
$headers = $client->getHeaders();
120+
$this->assertFalse(array_key_exists('resultLimit', $headers));;
121+
$client->setResultLimit(111, 999);
122+
$headers = $client->getHeaders();
123+
$this->assertTrue(array_key_exists('resultLimit', $headers));
124+
$this->assertEquals(111, $headers['resultLimit']->data->limit);
125+
$this->assertEquals(999, $headers['resultLimit']->data->offset);
50126
}
127+
51128
}
129+

0 commit comments

Comments
 (0)
Please sign in to comment.