Skip to content

Commit 4d1d163

Browse files
committed
update php template to debug curl
1 parent 171bf96 commit 4d1d163

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,25 @@ class APIClient {
235235
// Set user agent
236236
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
237237

238+
// debugging for curl
239+
if (Configuration::$debug) {
240+
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
241+
242+
curl_setopt($curl, CURLOPT_VERBOSE, 1);
243+
curl_setopt($curl, CURLOPT_STDERR, fopen(Configuration::$debug_file, 'a'));
244+
} else {
245+
curl_setopt($curl, CURLOPT_VERBOSE, 0);
246+
}
247+
238248
// Make the request
239249
$response = curl_exec($curl);
240250
$response_info = curl_getinfo($curl);
241251

252+
// debug HTTP response body
253+
if (Configuration::$debug) {
254+
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($response, true)."\n~END~\n", 3, Configuration::$debug_file);
255+
}
256+
242257
// Handle the response
243258
if ($response_info['http_code'] == 0) {
244259
throw new APIClientException("TIMEOUT: api call to " . $url .

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class Configuration {
3434
// an instance of APIClient
3535
public static $apiClient;
3636
37+
// debugging
38+
public static $debug = false; // by default debugging is disabled
39+
public static $debug_file = 'php://output'; //output debug log to STDOUT by default
40+
3741
/*
3842
* manually initalize API client
3943
*/

samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,7 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
245245
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
246246

247247
curl_setopt($curl, CURLOPT_VERBOSE, 1);
248-
try {
249-
$fp = fopen(Configuration::$debug_file, 'w');
250-
curl_setopt($curl, CURLOPT_STDERR, $fp);
251-
} catch ( \Exception $e ) {
252-
error_log("Exception in enabling curl debug: ".print_r($e, true), 3, Configuration::$debug_file);
253-
}
248+
curl_setopt($curl, CURLOPT_STDERR, fopen(Configuration::$debug_file, 'a'));
254249
} else {
255250
curl_setopt($curl, CURLOPT_VERBOSE, 0);
256251
}

samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Configuration {
3434
// an instance of APIClient
3535
public static $apiClient;
3636

37+
// debugging
3738
public static $debug = false; // by default debugging is disabled
3839
public static $debug_file = 'php://output'; //output debug log to STDOUT by default
3940

samples/client/petstore/php/test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// to enable logging
1010
//SwaggerClient\Configuration::$debug = true;
11+
//SwaggerClient\Configuration::$debug_file = '/var/tmp/php_debug.log';
1112

1213
$petId = 10005; // ID of pet that needs to be fetched
1314
try {

0 commit comments

Comments
 (0)