From 171bf969c7550cda408e8b20a96630dd330a17b7 Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 26 May 2015 23:20:34 +0800 Subject: [PATCH] add debug switch to configuration --- .../php/SwaggerClient-php/lib/APIClient.php | 20 +++++++++++++++++++ .../SwaggerClient-php/lib/Configuration.php | 3 +++ .../SwaggerClient-php/tests/PetApiTest.php | 3 +++ samples/client/petstore/php/test.php | 3 +++ 4 files changed, 29 insertions(+) diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php index f7002fba18d..bc243e852cb 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php @@ -240,10 +240,30 @@ class APIClient { // Set user agent curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); + // debugging for curl + if (Configuration::$debug) { + error_log("[DEBUG] HTTP Request body ~BEGIN~\n".print_r($postData, true)."\n~END~\n", 3, Configuration::$debug_file); + + curl_setopt($curl, CURLOPT_VERBOSE, 1); + try { + $fp = fopen(Configuration::$debug_file, 'w'); + curl_setopt($curl, CURLOPT_STDERR, $fp); + } catch ( \Exception $e ) { + error_log("Exception in enabling curl debug: ".print_r($e, true), 3, Configuration::$debug_file); + } + } else { + curl_setopt($curl, CURLOPT_VERBOSE, 0); + } + // Make the request $response = curl_exec($curl); $response_info = curl_getinfo($curl); + // debug HTTP response body + if (Configuration::$debug) { + error_log("[DEBUG] HTTP Response body ~BEGIN~\n".print_r($response, true)."\n~END~\n", 3, Configuration::$debug_file); + } + // Handle the response if ($response_info['http_code'] == 0) { throw new APIClientException("TIMEOUT: api call to " . $url . diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php index 1f91f8d9063..950b31ed587 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Configuration.php @@ -34,6 +34,9 @@ class Configuration { // an instance of APIClient public static $apiClient; + public static $debug = false; // by default debugging is disabled + public static $debug_file = 'php://output'; //output debug log to STDOUT by default + /* * manually initalize API client */ diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 39c5cecd018..85f0c900a6c 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -7,6 +7,9 @@ class PetApiTest extends \PHPUnit_Framework_TestCase // add a new pet (id 10005) to ensure the pet object is available for all the tests public static function setUpBeforeClass() { + // enable debugging + //SwaggerClient\Configuration::$debug = true; + // skip initializing the API client as it should be automatic //$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); // new pet diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 41c206ee6b0..22bea9db889 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -6,6 +6,9 @@ require_once('SwaggerClient-php/SwaggerClient.php'); //$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); //$api_client->addDefaultHeader("test1", "value1"); +// to enable logging +//SwaggerClient\Configuration::$debug = true; + $petId = 10005; // ID of pet that needs to be fetched try { //$pet_api = new SwaggerClient\PetAPI($api_client);