Skip to content

Commit acf8e9f

Browse files
committed
Include curl error code constant in curl error message (Curl::curlErrorMessage)
Before: Timeout was reached: Operation timed out after 30001 milliseconds with 1453867 out of 3000000 bytes received After: Timeout was reached (CURLE_OPERATION_TIMEOUTED): Operation timed out after 30001 milliseconds with 1453867 out of 3000000 bytes received
1 parent 9195bcd commit acf8e9f

25 files changed

+69
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ $curl = new Curl();
5252
$curl->get('https://www.example.com/');
5353

5454
if ($curl->error) {
55-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
55+
echo 'Error: ' . $curl->errorMessage . "\n";
5656
} else {
5757
echo 'Response:' . "\n";
5858
var_dump($curl->response);
@@ -85,7 +85,7 @@ $curl->setCookie('key', 'value');
8585
$curl->get('https://www.example.com/');
8686

8787
if ($curl->error) {
88-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
88+
echo 'Error: ' . $curl->errorMessage . "\n";
8989
} else {
9090
echo 'Response:' . "\n";
9191
var_dump($curl->response);

examples/before_send_retry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
$curl->get('https://httpbin.org/status/503');
1818

1919
if ($curl->error) {
20-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
20+
echo 'Error: ' . $curl->errorMessage . "\n";
2121
echo 'final attempts: ' . $curl->attempts . "\n";
2222
echo 'final retries: ' . $curl->retries . "\n";
2323
} else {

examples/custom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
$curl->exec();
1212

1313
if ($curl->error) {
14-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
14+
echo 'Error: ' . $curl->errorMessage . "\n";
1515
} else {
1616
echo 'Response:' . "\n";
1717
var_dump($curl->response);

examples/delete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
);
2020

2121
if ($curl->error) {
22-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
22+
echo 'Error: ' . $curl->errorMessage . "\n";
2323
} else {
2424
echo 'Data server received via DELETE:' . "\n";
2525
var_dump($curl->response->form);

examples/get.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
]);
1212

1313
if ($curl->error) {
14-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
14+
echo 'Error: ' . $curl->errorMessage . "\n";
1515
} else {
1616
echo 'Response:' . "\n";
1717
var_dump($curl->response);

examples/get_first_n_bytes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$curl->get('https://code.jquery.com/jquery-1.11.2.min.js');
1010

1111
if ($curl->error) {
12-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
12+
echo 'Error: ' . $curl->errorMessage . "\n";
1313
} else {
1414
var_dump($curl->responseHeaders['status-line']); // HTTP/1.1 206 Partial Content
1515
var_dump($curl->responseHeaders['content-length']); // 50

examples/get_response_cookies.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$curl->get('https://www.php.net/');
88

99
if ($curl->error) {
10-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
10+
echo 'Error: ' . $curl->errorMessage . "\n";
1111
} else {
1212
echo 'Response cookies:' . "\n";
1313
var_dump($curl->responseCookies);

examples/get_with_callable_retry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$curl->get('https://httpbin.org/status/503');
1313

1414
if ($curl->error) {
15-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
15+
echo 'Error: ' . $curl->errorMessage . "\n";
1616
echo 'attempts: ' . $curl->attempts . "\n";
1717
echo 'retries: ' . $curl->retries . "\n";
1818
} else {

examples/get_with_callable_retry_based_on_http_status_code.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$curl->get('https://httpbin.org/status/503');
1414

1515
if ($curl->error) {
16-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
16+
echo 'Error: ' . $curl->errorMessage . "\n";
1717
echo 'attempts: ' . $curl->attempts . "\n";
1818
echo 'retries: ' . $curl->retries . "\n";
1919
} else {

examples/get_with_port.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
]);
1212

1313
if ($curl->error) {
14-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
14+
echo 'Error: ' . $curl->errorMessage . "\n";
1515
} else {
1616
echo 'Response:' . "\n";
1717
var_dump($curl->response);

examples/get_with_retry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$curl->get('https://httpbin.org/status/503');
1111

1212
if ($curl->error) {
13-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
13+
echo 'Error: ' . $curl->errorMessage . "\n";
1414
echo 'attempts: ' . $curl->attempts . "\n";
1515
echo 'retries: ' . $curl->retries . "\n";
1616
} else {

examples/get_without_downloading_full_error_response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
$curl->get('https://www.example.com/large-500-error');
3232
if ($curl->error) {
33-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
33+
echo 'Error: ' . $curl->errorMessage . "\n";
3434
echo 'Response content-length: ' . $curl->responseHeaders['content-length'] . "\n";
3535
echo 'Actual response size downloaded: ' . $curl->getInfo(CURLINFO_SIZE_DOWNLOAD) . "\n";
3636
} else {

examples/google_spreadsheet_values_update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
$curl->put($url, $data);
6565

6666
if ($curl->error) {
67-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
67+
echo 'Error: ' . $curl->errorMessage . "\n";
6868
var_dump($curl);
6969
} else {
7070
var_dump($curl->response);

examples/head.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
]);
1212

1313
if ($curl->error) {
14-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
14+
echo 'Error: ' . $curl->errorMessage . "\n";
1515
} else {
1616
echo 'Response headers:' . "\n";
1717
var_dump($curl->responseHeaders);

examples/multi_curl_before_send_retry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
$multi_curl->complete(function ($instance) {
1818
if ($instance->error) {
19-
echo 'Error: ' . $instance->errorCode . ': ' . $instance->errorMessage . "\n";
19+
echo 'Error: ' . $instance->errorMessage . "\n";
2020
echo 'final attempts: ' . $instance->attempts . "\n";
2121
echo 'final retries: ' . $instance->retries . "\n";
2222
} else {

examples/options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
]);
1212

1313
if ($curl->error) {
14-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
14+
echo 'Error: ' . $curl->errorMessage . "\n";
1515
} else {
1616
echo 'Response:' . "\n";
1717
var_dump($curl->response);

examples/patch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]);
1414

1515
if ($curl->error) {
16-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
16+
echo 'Error: ' . $curl->errorMessage . "\n";
1717
} else {
1818
echo 'Response:' . "\n";
1919
var_dump($curl->response);

examples/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]);
1414

1515
if ($curl->error) {
16-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
16+
echo 'Error: ' . $curl->errorMessage . "\n";
1717
} else {
1818
echo 'Data server received via POST:' . "\n";
1919
var_dump($curl->response->form);

examples/progress.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
echo ' ' . $percent . '%' . "\r";
1616
});
1717
$curl->download('https://www.php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');
18+
19+
if ($curl->error) {
20+
echo 'Download error: ' . $curl->errorMessage . "\n";
21+
} else {
22+
echo 'Download complete' . "\n";
23+
}

examples/progress_advanced.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
echo ']' . "\r";
2828
});
2929
$curl->complete(function ($instance) {
30-
echo "\n" . 'download complete' . "\n";
30+
if ($instance->error) {
31+
echo "\n" . 'Download error: ' . $instance->errorMessage . "\n";
32+
} else {
33+
echo "\n" . 'Download complete' . "\n";
34+
}
3135
});
3236
$curl->download('https://www.php.net/distributions/manual/php_manual_en.html.gz', '/tmp/php_manual_en.html.gz');

examples/put.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]);
1414

1515
if ($curl->error) {
16-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
16+
echo 'Error: ' . $curl->errorMessage . "\n";
1717
} else {
1818
echo 'Data server received via PUT:' . "\n";
1919
var_dump($curl->response->form);

examples/search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]);
1414

1515
if ($curl->error) {
16-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
16+
echo 'Error: ' . $curl->errorMessage . "\n";
1717
} else {
1818
echo 'Response:' . "\n";
1919
var_dump($curl->response);

examples/upload_file.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
]);
1818

1919
if ($curl->error) {
20-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
20+
echo 'Error: ' . $curl->errorMessage . "\n";
2121
} else {
2222
echo 'Success' . "\n";
2323
}

examples/use_custom_xml_decoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$curl->get('https://httpbin.org/xml');
1717

1818
if ($curl->error) {
19-
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
19+
echo 'Error: ' . $curl->errorMessage . "\n";
2020
} else {
2121
echo 'Response:' . "\n";
2222
var_dump($curl->response);

src/Curl/Curl.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class Curl
9797
];
9898

9999
private static $deferredProperties = [
100+
'curlErrorCodeConstant',
100101
'effectiveUrl',
101102
'rfc2616',
102103
'rfc6265',
@@ -519,10 +520,17 @@ public function exec($ch = null)
519520

520521
// Include additional error code information in error message when possible.
521522
if ($this->curlError) {
522-
$this->curlErrorMessage =
523-
curl_strerror($this->curlErrorCode) . (
524-
empty($this->curlErrorMessage) ? '' : ': ' . $this->curlErrorMessage
525-
);
523+
$curl_error_message = curl_strerror($this->curlErrorCode);
524+
525+
if ($this->curlErrorCodeConstant !== '') {
526+
$curl_error_message .= ' (' . $this->curlErrorCodeConstant . ')';
527+
}
528+
529+
if (!empty($this->curlErrorMessage)) {
530+
$curl_error_message .= ': ' . $this->curlErrorMessage;
531+
}
532+
533+
$this->curlErrorMessage = $curl_error_message;
526534
}
527535

528536
$this->httpStatusCode = $this->getInfo(CURLINFO_HTTP_CODE);
@@ -547,6 +555,7 @@ public function exec($ch = null)
547555
$this->errorMessage = $this->curlError ? $this->curlErrorMessage : $this->httpErrorMessage;
548556

549557
// Reset select deferred properties so that they may be recalculated.
558+
unset($this->curlErrorCodeConstant);
550559
unset($this->effectiveUrl);
551560
unset($this->totalTime);
552561

@@ -1832,6 +1841,28 @@ public function __get($name)
18321841
return $return;
18331842
}
18341843

1844+
/**
1845+
* Get Curl Error Code Constant
1846+
*
1847+
* @access private
1848+
*/
1849+
private function _get_curlErrorCodeConstant()
1850+
{
1851+
$constants = get_defined_constants(true);
1852+
$filtered_array = array_filter(
1853+
$constants['curl'],
1854+
function ($key) {
1855+
return strpos($key, 'CURLE_') !== false;
1856+
},
1857+
ARRAY_FILTER_USE_KEY
1858+
);
1859+
$curl_const_by_code = array_flip($filtered_array);
1860+
if (isset($curl_const_by_code[$this->curlErrorCode])) {
1861+
return $curl_const_by_code[$this->curlErrorCode];
1862+
}
1863+
return '';
1864+
}
1865+
18351866
/**
18361867
* Get Effective Url
18371868
*

0 commit comments

Comments
 (0)