Skip to content

Commit 171bf96

Browse files
committed
add debug switch to configuration
1 parent 095771d commit 171bf96

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,30 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
240240
// Set user agent
241241
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
242242

243+
// debugging for curl
244+
if (Configuration::$debug) {
245+
error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file);
246+
247+
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+
}
254+
} else {
255+
curl_setopt($curl, CURLOPT_VERBOSE, 0);
256+
}
257+
243258
// Make the request
244259
$response = curl_exec($curl);
245260
$response_info = curl_getinfo($curl);
246261

262+
// debug HTTP response body
263+
if (Configuration::$debug) {
264+
error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($response, true)."\n~END~\n", 3, Configuration::$debug_file);
265+
}
266+
247267
// Handle the response
248268
if ($response_info['http_code'] == 0) {
249269
throw new APIClientException("TIMEOUT: api call to " . $url .

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

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

37+
public static $debug = false; // by default debugging is disabled
38+
public static $debug_file = 'php://output'; //output debug log to STDOUT by default
39+
3740
/*
3841
* manually initalize API client
3942
*/

samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
77

88
// add a new pet (id 10005) to ensure the pet object is available for all the tests
99
public static function setUpBeforeClass() {
10+
// enable debugging
11+
//SwaggerClient\Configuration::$debug = true;
12+
1013
// skip initializing the API client as it should be automatic
1114
//$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
1215
// new pet

samples/client/petstore/php/test.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
//$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
77
//$api_client->addDefaultHeader("test1", "value1");
88

9+
// to enable logging
10+
//SwaggerClient\Configuration::$debug = true;
11+
912
$petId = 10005; // ID of pet that needs to be fetched
1013
try {
1114
//$pet_api = new SwaggerClient\PetAPI($api_client);

0 commit comments

Comments
 (0)