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

Explanation and Practical Steps for Using the curl_share_errno Function

gitbox 2025-09-21

<?php
echo "

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 for shared cURL handles. cURL provides powerful networking capabilities, and shared handles (curl share) allow multiple cURL handles to share data such as cookies, DNS cache, etc., improving request efficiency. When there is an issue with the shared handle, the curl_share_errno function can be used to obtain specific error codes, which helps in 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 description:

"
;
echo ""; echo "

Return value: Returns an integer error code. If there is no error, it returns CURLSHE_OK (usually 0).

"
;

echo "

2. Usage Scenarios

"
;
echo "

When sharing cookies or DNS cache across multiple cURL requests, if you encounter request failures or shared data issues, you can use curl_share_errno to obtain the specific error code, helping to quickly locate the problem.

"
;

echo "

3. Practical 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 "

This step sets the shared handle 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 functioning 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. Important Notes

"
;
echo "

  • Ensure that you close the shared handle only after all requests have been completed.
  • The error codes returned by curl_share_errno can be looked up through constants or documentation, such as CURLSHE_OK, CURLSHE_BAD_OPTION, etc.
  • Shared handles are mainly used for sharing data such as cookies, DNS, and SSL sessions, avoiding the performance overhead of re-initializing multiple times.
"
;

echo "

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

"
;
?>