Current Location: Home> Latest Articles> curl_error() and curl_errno() skills to debug sharing failure problems

curl_error() and curl_errno() skills to debug sharing failure problems

M66 2025-05-31

When using PHP's cURL library to make network requests, the curl_share_init() function is used to initialize a shared handle so that multiple cURL sessions can share resources, such as DNS caches, cookies, etc. This helps save resources and improve efficiency in high concurrency environments. However, many developers may encounter sharing failure problems when using curl_share_init() , which is often related to resource conflicts, misconfiguration, or problems with underlying libraries (libcurl).

This article will take you to understand how to locate and solve the sharing failure problems caused by curl_share_init through the two debugging tools , curl_error() and curl_errno() .

1?? What is curl_share_init()?

curl_share_init() is used to create a shared handle, which is usually used in conjunction with curl_share_setopt() to define the type of resource to be shared. Finally, you need to use curl_share_close() to release the handle.

Simple example:

 $sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);

However, if configured or used incorrectly, some sharing operations may fail, manifesting as request exceptions, data confusion, or even crashes.

2?? Common reasons for sharing failure

Common causes of failure include:

  • Use the same cURL handle in different threads;

  • Repeat the shared handle or handle repeatedly;

  • The set sharing types (such as COOKIE, DNS) are not supported at the bottom;

  • The libcurl version is incompatible.

To locate specific errors, we need to use curl_error() and curl_errno() .

3?? Debugging skills of curl_error() and curl_errno()

These two functions are the core of cURL error debugging:

  • curl_error($ch) returns the error message (in string form) of the last cURL operation;

  • curl_errno($ch) returns the error code (numerical form) of the last cURL operation.

They are usually used together:

 $ch = curl_init('https://m66.net/api/test');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

if ($result === false) {
    echo 'cURL Error Code: ' . curl_errno($ch) . "\n";
    echo 'cURL Error Message: ' . curl_error($ch) . "\n";
} else {
    echo 'Request successful: ' . $result;
}

curl_close($ch);

For shared handles:

 $sh = curl_share_init();
if ($sh === false) {
    die("Failed to initialize curl share handle.\n");
}

$setopt_result = curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
if ($setopt_result === false) {
    echo "Failed to set share option.\n";
}

$ch = curl_init('https://m66.net/api/test');
curl_setopt($ch, CURLOPT_SHARE, $sh);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

if ($result === false) {
    echo 'cURL Error Code: ' . curl_errno($ch) . "\n";
    echo 'cURL Error Message: ' . curl_error($ch) . "\n";
} else {
    echo 'Request successful: ' . $result;
}

curl_close($ch);
curl_share_close($sh);

4?? Common error codes and troubleshooting ideas

Error code Common reasons
1 CURLE_UNSUPPORTED_PROTOCOL
3 CURLE_URL_MALFORMAT
5 CURLE_COULDNT_RESOLVE_PROXY
6 CURLE_COULDNT_RESOLVE_HOST
7 CURLE_COULDNT_CONNECT
twenty three CURLE_WRITE_ERROR
43 CURLE_BAD_FUNCTION_ARGUMENT
81 CURLE_SHARE_IN_USE

For CURLE_SHARE_IN_USE , it means that the current shared handle is being used by another handle and cannot be configured or released at the same time. The code logic needs to be checked.

5?? Practical investigation steps

1?? Confirm that the libcurl version supports you can use curl_version() to check the currently supported features.

2?? To separate the scope of the problem, first use a single cURL handle to test it, and then introduce a shared handle to confirm that it is a problem caused by share.

3?? Use curl_error() and curl_errno() to obtain detailed errors must be checked immediately after curl_exec() .

4?? Enable detailed logs through CURLOPT_VERBOSE :

 curl_setopt($ch, CURLOPT_VERBOSE, true);

5?? Check thread safety Do not share handles in multiple threads unless libcurl is thread-safely compiled.