Current Location: Home> Latest Articles> Detailed Explanation and Practical Steps for Using the curl_share_errno Function

Detailed Explanation and Practical Steps for Using the curl_share_errno Function

gitbox 2025-09-21

<?php
echo "

Detailed Explanation and Practical Steps for Using the curl_share_errno Function

";

echo "

In PHP, curl_share_errno is a function specifically used to retrieve error codes from a shared cURL handle. cURL provides powerful network request capabilities, and shared handles (curl share) allow multiple cURL handles to share data such as cookies, DNS cache, etc., improving request efficiency. When a shared handle encounters an issue, curl_share_errno can be used to obtain the specific error code, facilitating debugging and exception handling.

"
;

echo "

1. Function Definition

"
;
echo "

The function prototype is as follows:

"
;
echo "
int curl_share_errno(resource $share_handle)
"
;
echo "

Parameter Explanation:

"
;
echo ""; echo "

Return Value: The function returns an integer error code. If no error occurs, it returns CURLSHE_OK (typically 0).

"
;

echo "

2. Usage Scenarios

"
;
echo "

When sharing cookies or DNS cache across multiple cURL requests, if any request fails or shared data is abnormal, curl_share_errno can be used to retrieve the specific error, helping to quickly pinpoint the problem.

"
;

echo "

3. Practical Operation Steps

"
;

echo "

Step 1: Initialize the Shared Handle

"
;
echo "
<br>
$sh = curl_share_init();<br>
"
;

echo "

Step 2: Set Shared Options

"
;
echo "
<br>
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);<br>
"
;
echo "

Here, the shared handle is set to share cookie data.

"
;

echo "

Step 3: Create cURL Requests and Use the Shared Handle

"
;
echo "
<br>
$ch1 = curl_init('<a data-is-only-node="" rel="noopener" target="_new" class="decorated-link" href="https://example.com">https://example.com<span aria-hidden="true" class="ms-0.5 inline-block align-middle leading-none"><svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" data-rtl-flip="" class="block h-[0.75em] w-[0.75em] stroke-current stroke-[0.75]"><path d="M14.3349 13.3301V6.60645L5.47065 15.4707C5.21095 15.7304 4.78895 15.7304 4.52925 15.4707C4.26955 15.211 4.26955 14.789 4.52925 14.5293L13.3935 5.66504H6.66011C6.29284 5.66504 5.99507 5.36727 5.99507 5C5.99507 4.63273 6.29284 4.33496 6.66011 4.33496H14.9999L15.1337 4.34863C15.4369 4.41057 15.665 4.67857 15.665 5V13.3301C15.6649 13.6973 15.3672 13.9951 14.9999 13.9951C14.6327 13.9951 14.335 13.6973 14.3349 13.3301Z"></path></svg></span></a>');<br>
curl_setopt($ch1, CURLOPT_SHARE, $sh);<br>
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);<br>
$response1 = curl_exec($ch1);</p>
<p>$ch2 = curl_init('<a rel="noopener" target="_new" class="decorated-link" href="https://example.org">https://example.org<span aria-hidden="true" class="ms-0.5 inline-block align-middle leading-none"><svg width="20" height="20" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" data-rtl-flip="" class="block h-[0.75em] w-[0.75em] stroke-current stroke-[0.75]"><path d="M14.3349 13.3301V6.60645L5.47065 15.4707C5.21095 15.7304 4.78895 15.7304 4.52925 15.4707C4.26955 15.211 4.26955 14.789 4.52925 14.5293L13.3935 5.66504H6.66011C6.29284 5.66504 5.99507 5.36727 5.99507 5C5.99507 4.63273 6.29284 4.33496 6.66011 4.33496H14.9999L15.1337 4.34863C15.4369 4.41057 15.665 4.67857 15.665 5V13.3301C15.6649 13.6973 15.3672 13.9951 14.9999 13.9951C14.6327 13.9951 14.335 13.6973 14.3349 13.3301Z"></path></svg></span></a>');<br>
curl_setopt($ch2, CURLOPT_SHARE, $sh);<br>
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);<br>
$response2 = curl_exec($ch2);<br>
"
;

echo "

Step 4: Check for Shared Handle Errors

"
;
echo "
<br>
$error_code = curl_share_errno($sh);<br>
if ($error_code !== CURLSHE_OK) {<br>
echo 'Shared handle error code: ' . $error_code;<br>
} else {<br>
echo 'Shared handle is running normally';<br>
}<br>
"
;

echo "

Step 5: Release Resources

"
;
echo "
<br>
curl_share_close($sh);<br>
curl_close($ch1);<br>
curl_close($ch2);<br>
"
;

echo "

4. Notes

"
;
echo "

  • When using shared handles, ensure that the shared handle is closed only after all requests are completed.
  • The error codes returned by curl_share_errno can be checked through constants or documentation, such as CURLSHE_OK, CURLSHE_BAD_OPTION, etc.
  • Shared handles are primarily used for sharing cookie, DNS, and SSL session data, reducing the performance overhead caused by repeated initialization.
"
;

echo "

Summary: By using curl_share_errno, developers can easily check and debug exceptions in shared cURL handles across multiple requests, improving the stability and performance of PHP network request programs.

"
;
?>