mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-10-17 09:25:50 +00:00
Add method to run basic request diagnostics
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user