Skip to content

Commit ecbe65a

Browse files
authored
Merge pull request php-curl-class#385 from zachborboa/master
Include additional information in error message
2 parents d210489 + c118e61 commit ecbe65a

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ $curl->get('https://www.example.com/');
7070

7171
if ($curl->error) {
7272
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage;
73-
}
74-
else {
73+
} else {
7574
echo $curl->response;
7675
}
7776

@@ -149,7 +148,8 @@ $multi_curl = new MultiCurl();
149148

150149
$multi_curl->success(function($instance) {
151150
echo 'call to "' . $instance->url . '" was successful.' . "\n";
152-
echo 'response: ' . $instance->response . "\n";
151+
echo 'response:' . "\n";
152+
var_dump($instance->response);
153153
});
154154
$multi_curl->error(function($instance) {
155155
echo 'call to "' . $instance->url . '" was unsuccessful.' . "\n";

src/Curl/Curl.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public function close()
204204
$this->options = null;
205205
$this->jsonDecoder = null;
206206
$this->xmlDecoder = null;
207+
$this->defaultDecoder = null;
207208
}
208209

209210
/**
@@ -340,11 +341,18 @@ public function exec($ch = null)
340341
$this->call($this->beforeSendFunction);
341342
$this->rawResponse = curl_exec($this->curl);
342343
$this->curlErrorCode = curl_errno($this->curl);
344+
$this->curlErrorMessage = curl_error($this->curl);
343345
} else {
344346
$this->rawResponse = curl_multi_getcontent($ch);
347+
$this->curlErrorMessage = curl_error($ch);
345348
}
346-
$this->curlErrorMessage = curl_error($this->curl);
347349
$this->curlError = !($this->curlErrorCode === 0);
350+
351+
// Include additional error code information in error message when possible.
352+
if ($this->curlError && function_exists('curl_strerror')) {
353+
$this->curlErrorMessage = curl_strerror($this->curlErrorCode) . ': ' . $this->curlErrorMessage;
354+
}
355+
348356
$this->httpStatusCode = $this->getInfo(CURLINFO_HTTP_CODE);
349357
$this->httpError = in_array(floor($this->httpStatusCode / 100), array(4, 5));
350358
$this->error = $this->curlError || $this->httpError;

src/Curl/MultiCurl.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ public function start()
536536
if ($info_array['msg'] === CURLMSG_DONE) {
537537
foreach ($this->curls as $key => $ch) {
538538
if ($ch->curl === $info_array['handle']) {
539+
// Set the error code for multi handles using the "result" key in the array returned by
540+
// curl_multi_info_read(). Using curl_errno() on a multi handle will incorrectly return 0
541+
// for errors.
539542
$ch->curlErrorCode = $info_array['result'];
540543
$ch->exec($ch->curl);
541544
curl_multi_remove_handle($this->multiCurl, $ch->curl);

0 commit comments

Comments
 (0)