Skip to content

Commit 97a5a89

Browse files
committed
Allow to disable SSL verification
This pull request adds the possibility to disable SSL verification for hosts using a self-signed SSL certificate
1 parent 860b551 commit 97a5a89

File tree

2 files changed

+124
-86
lines changed

2 files changed

+124
-86
lines changed

modules/swagger-codegen/src/main/resources/php/ApiClient.mustache

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
/**
33
* ApiClient
4-
*
4+
*
55
* PHP version 5
66
*
77
* @category Class
8-
* @package {{invokerPackage}}
8+
* @package {{invokerPackage}}
99
* @author http://github.com/swagger-api/swagger-codegen
1010
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2
1111
* @link https://github.com/swagger-api/swagger-codegen
@@ -26,8 +26,8 @@
2626
* limitations under the License.
2727
*/
2828
/**
29-
* NOTE: This class is auto generated by the swagger code generator program.
30-
* https://github.com/swagger-api/swagger-codegen
29+
* NOTE: This class is auto generated by the swagger code generator program.
30+
* https://github.com/swagger-api/swagger-codegen
3131
* Do not edit the class manually.
3232
*/
3333

@@ -37,7 +37,7 @@ namespace {{invokerPackage}};
3737
* ApiClient Class Doc Comment
3838
*
3939
* @category Class
40-
* @package {{invokerPackage}}
40+
* @package {{invokerPackage}}
4141
* @author http://github.com/swagger-api/swagger-codegen
4242
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2
4343
* @link https://github.com/swagger-api/swagger-codegen
@@ -52,19 +52,19 @@ class ApiClient
5252
public static $OPTIONS = "OPTIONS";
5353
public static $PUT = "PUT";
5454
public static $DELETE = "DELETE";
55-
56-
/**
55+
56+
/**
5757
* Configuration
5858
* @var Configuration
5959
*/
6060
protected $config;
61-
61+
6262
/**
6363
* Object Serializer
6464
* @var ObjectSerializer
6565
*/
6666
protected $serializer;
67-
67+
6868
/**
6969
* Constructor of the class
7070
* @param Configuration $config config for this ApiClient
@@ -74,11 +74,11 @@ class ApiClient
7474
if ($config == null) {
7575
$config = Configuration::getDefaultConfiguration();
7676
}
77-
77+
7878
$this->config = $config;
7979
$this->serializer = new ObjectSerializer();
8080
}
81-
81+
8282
/**
8383
* Get the config
8484
* @return Configuration
@@ -87,7 +87,7 @@ class ApiClient
8787
{
8888
return $this->config;
8989
}
90-
90+
9191
/**
9292
* Get the serializer
9393
* @return ObjectSerializer
@@ -96,7 +96,7 @@ class ApiClient
9696
{
9797
return $this->serializer;
9898
}
99-
99+
100100
/**
101101
* Get API key (with prefix if set)
102102
* @param string $apiKeyIdentifier name of apikey
@@ -106,20 +106,20 @@ class ApiClient
106106
{
107107
$prefix = $this->config->getApiKeyPrefix($apiKeyIdentifier);
108108
$apiKey = $this->config->getApiKey($apiKeyIdentifier);
109-
109+
110110
if (!isset($apiKey)) {
111111
return null;
112112
}
113-
113+
114114
if (isset($prefix)) {
115115
$keyWithPrefix = $prefix." ".$apiKey;
116116
} else {
117117
$keyWithPrefix = $apiKey;
118118
}
119-
119+
120120
return $keyWithPrefix;
121121
}
122-
122+
123123
/**
124124
* Make the HTTP call (Sync)
125125
* @param string $resourcePath path to method endpoint
@@ -133,42 +133,48 @@ class ApiClient
133133
*/
134134
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType=null)
135135
{
136-
136+
137137
$headers = array();
138-
138+
139139
// construct the http header
140140
$headerParams = array_merge(
141-
(array)$this->config->getDefaultHeaders(),
141+
(array)$this->config->getDefaultHeaders(),
142142
(array)$headerParams
143143
);
144-
144+
145145
foreach ($headerParams as $key => $val) {
146146
$headers[] = "$key: $val";
147147
}
148-
148+
149149
// form data
150150
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
151151
$postData = http_build_query($postData);
152152
} else if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
153153
$postData = json_encode($this->serializer->sanitizeForSerialization($postData));
154154
}
155-
155+
156156
$url = $this->config->getHost() . $resourcePath;
157-
157+
158158
$curl = curl_init();
159159
// set timeout, if needed
160160
if ($this->config->getCurlTimeout() != 0) {
161161
curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
162162
}
163163
// return the result on success, rather than just TRUE
164164
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
165-
165+
166166
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
167-
167+
168+
// disable SSL verification, if needed
169+
if ($this->config->getSSLVerification() == false) {
170+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
171+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
172+
}
173+
168174
if (! empty($queryParams)) {
169175
$url = ($url . '?' . http_build_query($queryParams));
170176
}
171-
177+
172178
if ($method == self::$POST) {
173179
curl_setopt($curl, CURLOPT_POST, true);
174180
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
@@ -190,44 +196,44 @@ class ApiClient
190196
throw new ApiException('Method ' . $method . ' is not recognized.');
191197
}
192198
curl_setopt($curl, CURLOPT_URL, $url);
193-
199+
194200
// Set user agent
195201
curl_setopt($curl, CURLOPT_USERAGENT, $this->config->getUserAgent());
196-
202+
197203
// debugging for curl
198204
if ($this->config->getDebug()) {
199205
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, $this->config->getDebugFile());
200-
206+
201207
curl_setopt($curl, CURLOPT_VERBOSE, 1);
202208
curl_setopt($curl, CURLOPT_STDERR, fopen($this->config->getDebugFile(), 'a'));
203209
} else {
204210
curl_setopt($curl, CURLOPT_VERBOSE, 0);
205211
}
206-
212+
207213
// obtain the HTTP response headers
208214
curl_setopt($curl, CURLOPT_HEADER, 1);
209-
215+
210216
// Make the request
211217
$response = curl_exec($curl);
212218
$http_header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
213219
$http_header = substr($response, 0, $http_header_size);
214220
$http_body = substr($response, $http_header_size);
215221
$response_info = curl_getinfo($curl);
216-
222+
217223
// debug HTTP response body
218224
if ($this->config->getDebug()) {
219225
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($http_body, true)."\n~END~\n", 3, $this->config->getDebugFile());
220226
}
221-
227+
222228
// Handle the response
223229
if ($response_info['http_code'] == 0) {
224230
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
225231
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
226-
// return raw body if response is a file
232+
// return raw body if response is a file
227233
if ($responseType == '\SplFileObject') {
228234
return array($http_body, $http_header);
229235
}
230-
236+
231237
$data = json_decode($http_body);
232238
if (json_last_error() > 0) { // if response is a string
233239
$data = $http_body;
@@ -240,7 +246,7 @@ class ApiClient
240246
}
241247
return array($data, $http_header);
242248
}
243-
249+
244250
/**
245251
* Return the header 'Accept' based on an array of Accept provided
246252
*
@@ -258,7 +264,7 @@ class ApiClient
258264
return implode(',', $accept);
259265
}
260266
}
261-
267+
262268
/**
263269
* Return the content type based on an array of content-type provided
264270
*

0 commit comments

Comments
 (0)