doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

curl_multi: Unable to get error code

Open ichaoX opened this issue 1 year ago • 5 comments
trafficstars

Description

The following code:

<?php

$ch = curl_init("http://127.0.0.1:12345");

$mh = curl_multi_init();

curl_multi_add_handle($mh, $ch);

do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while ($running > 0);

var_dump(curl_errno($ch), curl_multi_errno($mh));

curl_multi_remove_handle($mh, $ch);


curl_exec($ch);

var_dump(curl_errno($ch), curl_error($ch));

Resulted in this output:

int(0)
int(0)
int(7)
string(81) "Failed to connect to 127.0.0.1 port 12345 after 0 ms: Could not connect to server"

But I expected this output instead:

int(7)
int(7) // I'm not sure.
int(7)
string(81) "Failed to connect to 127.0.0.1 port 12345 after 0 ms: Could not connect to server"

PHP Version

PHP 8.2.23

Operating System

No response

ichaoX avatar Oct 25 '24 07:10 ichaoX

I think this works as expected. curl_multi_errno() reports errors regarding the multi handle itself; errors for individual handles which have been added to the multi handle are not propagated. @TimWolla, am I correct? If so, that might need a bit of clarification in the docs.

Anyway, use curl_multi_info_read() to get information about erros on individual handles. See https://www.php.net/manual/en/function.curl-multi-exec.php for a full fledged example.

cmb69 avatar Oct 27 '24 17:10 cmb69

I think this works as expected. curl_multi_errno() reports errors regarding the multi handle itself; errors for individual handles which have been added to the multi handle are not propagated

Yes. The errno is only filled by the curl_multi_* functions. For curl_multi_exec(), which uses curl_multi_perform() internally, the curl documentation states:

This function returns errors regarding the whole multi stack. Problems on individual transfers may have occurred even when this function returns CURLM_OK. Use curl_multi_info_read to figure out how individual transfers did.

TimWolla avatar Oct 27 '24 17:10 TimWolla

Thank you for your explanation. It seems that the doc-en examples were recently updated.

What I was missing was curl_multi_info_read(); adding it allows me to get the error code.

while (curl_multi_info_read($mh) !== false);

var_dump(curl_errno($ch)); // int(7)

Maybe it would be more intuitive to get the error without needing curl_multi_info_read().

ichaoX avatar Oct 27 '24 20:10 ichaoX

Maybe it would be more intuitive to get the error without needing curl_multi_info_read().

Possibly. But given that ext/curl is a rather thin wrapper over libcurl, I don't see that we go this route.

cmb69 avatar Nov 19 '24 19:11 cmb69

Oh, that has already been moved to doc-en. Reopening.

cmb69 avatar Nov 19 '24 19:11 cmb69