From 2a899465c4f22e5eed6be8acbc7581854a4e2f5b Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Sun, 18 Jul 2021 21:56:03 -0400 Subject: [PATCH] Add method to run basic request diagnostics --- README.md | 1 + TROUBLESHOOTING.md | 8 +++ src/Curl/Curl.php | 83 +++++++++++++++++++++++++ tests/PHPCurlClass/PHPCurlClassTest.php | 24 +++++++ 4 files changed, 116 insertions(+) diff --git a/README.md b/README.md index 9aa39a6..653e944 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ Curl::call() Curl::close() Curl::complete($callback) Curl::delete($url, $query_parameters = [], $data = []) +Curl::diagnose() Curl::disableTimeout() Curl::download($url, $mixed_filename) Curl::error($callback) diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index ffa285e..f4cc50e 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -1,5 +1,13 @@ # Troubleshooting +### Debug using the diagnose method + +```php +$curl = new Curl(); +$curl->get('https://www.example.com/'); +$curl->diagnose(); // <-- HERE +``` + ### Debug the entire curl instance ```php diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 9a83301..750acdb 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -1432,6 +1432,89 @@ class Curl $this->setOpt(CURLOPT_STDERR, $output); } + /** + * Diagnose + * + * @access public + */ + public function diagnose() + { + echo "\n"; + echo '--- Begin PHP Curl Class diagnostic output ---' . "\n"; + echo 'PHP Curl Class version: ' . self::VERSION . "\n"; + echo 'PHP version: ' . PHP_VERSION . "\n"; + + $curl_version = curl_version(); + echo 'Curl version: ' . $curl_version['version'] . "\n"; + + if ($this->attempts === 0) { + echo 'No HTTP requests have been made.' . "\n"; + } else { + $request_method = $this->getOpt(CURLOPT_CUSTOMREQUEST); + $request_url = $this->getOpt(CURLOPT_URL); + $request_headers_count = count($this->requestHeaders); + $request_body_empty = empty($this->getOpt(CURLOPT_POSTFIELDS)); + $response_length = isset($this->responseHeaders['Content-Length']) ? + $this->responseHeaders['Content-Length'] : '(not specified in response)'; + $response_headers_count = count($this->responseHeaders); + + echo + 'Sent an HTTP ' . $request_method . ' request to "' . $request_url . '".' . "\n" . + 'Request contained ' . ( + $request_headers_count === 1 ? 'no headers.' : + $request_headers_count . ' headers:' + ) . "\n"; + if ($this->requestHeaders !== null) { + $i = 1; + foreach ($this->requestHeaders as $key => $value) { + echo ' ' . $i . ' ' . $key . ': ' . $value . "\n"; + $i += 1; + } + } + + echo 'Request contained ' . ($request_body_empty ? 'no body' : 'a body') . '.' . "\n"; + + echo + 'Received response containing ' . ( + $response_headers_count === 1 ? 'no headers.' : + $response_headers_count . ' headers:' + ) . "\n"; + if ($this->responseHeaders !== null) { + $i = 1; + foreach ($this->responseHeaders as $key => $value) { + echo ' ' . $i . ' ' . $key . ': ' . $value . "\n"; + $i += 1; + } + } + + if ($this->curlError) { + echo + 'A curl error (' . $this->curlErrorCode . ') occurred ' . + 'with message "' . $this->curlErrorMessage . '".' . "\n"; + } + if (!empty($this->httpStatusCode)) { + echo 'Received an HTTP status code of ' . $this->httpStatusCode . '.' . "\n"; + } + if ($this->httpError) { + echo + 'Received an HTTP ' . $this->httpStatusCode . ' error response ' . + 'with message "' . $this->httpErrorMessage . '".' . "\n"; + } + + if ($this->response === null) { + echo 'Received no response body.' . "\n"; + } elseif ($this->response === '') { + echo 'Received an empty response body.' . "\n"; + } else { + echo 'Received a non-empty response body.' . "\n"; + echo 'Response content length: ' . $response_length . "\n"; + } + } + + echo '--- End PHP Curl Class diagnostic output ---' . "\n"; + echo "\n"; + } + /** * Reset * diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 2ed5740..52b1b01 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -4027,4 +4027,28 @@ class CurlTest extends \PHPUnit\Framework\TestCase $curl->setMaximumRedirects(3); $this->assertEquals(3, $curl->getOpt(CURLOPT_MAXREDIRS)); } + + public function testDiagnose() + { + $test = new Test(); + $test->server('error_message', 'GET'); + + ob_start(); + $test->curl->diagnose(); + $output = ob_get_contents(); + ob_end_clean(); + + $this->assertStringContainsString('--- Begin PHP Curl Class diagnostic output ---', $output); + $this->assertStringContainsString('PHP Curl Class version: ' . Curl::VERSION, $output); + $this->assertStringContainsString('PHP version: ' . PHP_VERSION, $output); + $this->assertStringContainsString('Sent an HTTP GET request ', $output); + $this->assertStringContainsString('Request contained no body.', $output); + $this->assertStringContainsString('Received an HTTP status code of 401.', $output); + $this->assertStringContainsString( + 'Received an HTTP 401 error response with message "HTTP/1.1 401 Unauthorized".', + $output + ); + $this->assertStringContainsString('Received an empty response body.', $output); + $this->assertStringContainsString('--- End PHP Curl Class diagnostic output ---', $output); + } }