Current Location: Home> Latest Articles> Are curl_share_init() and curl_init() the same thing?

Are curl_share_init() and curl_init() the same thing?

M66 2025-06-06

curl_init() is one of the most commonly used cURL functions. Its function is to initialize a cURL session handle for various subsequent cURL operations. Every time curl_init() is used, a cURL handle is returned, which is the basis for subsequent operations. Through it, we can set request options, execute requests, get responses, etc.

Basic usage:

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://m66.net/api/endpoint");  // Set requestedURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // Set the return value to a string,Instead of outputting directly
$response = curl_exec($ch);  // Execute a request and get a response
curl_close($ch);  // closure cURL Handle

curl_init() is mainly used to initialize cURL requests. It does not involve sharing settings between multiple requests. Therefore, every time a new request is initiated, curl_init() needs to be called to create a new session handle.

2. curl_share_init() function

Unlike curl_init() , curl_share_init() is a function used to create a shared resource, which returns a shared handle. This handle can share resources in multiple cURL sessions, such as connections, cookies, DNS lookups, etc. It is used to implement sharing settings across multiple cURL sessions, often used when making concurrent requests to reduce duplicate workload and improve performance.

Basic usage:

 $ch1 = curl_init();
$ch2 = curl_init();

// 创建共享Handle
$sh = curl_share_init();

// 设置共享Handle的资源
curl_setopt($ch1, CURLOPT_SHARE, $sh);
curl_setopt($ch2, CURLOPT_SHARE, $sh);

// Set Other cURL Request Options
curl_setopt($ch1, CURLOPT_URL, "https://m66.net/api/endpoint1");
curl_setopt($ch2, CURLOPT_URL, "https://m66.net/api/endpoint2");

curl_exec($ch1);
curl_exec($ch2);

// closure会话和共享Handle
curl_close($ch1);
curl_close($ch2);
curl_share_close($sh);

In this example, the shared handle $sh returned by curl_share_init() is set to $ch1 and $ch2 in two different cURL sessions, so that they can share resources. curl_share_init() is mainly used to improve performance when requesting in parallel, and it allows multiple cURL sessions to share the same configuration.

3. The difference between curl_init() and curl_share_init()

Points of difference curl_init() curl_share_init()
Function Initialize a cURL session handle to initiate a single HTTP request Initialize a shared resource handle for multiple sessions to share resources
Return value Return a cURL handle Returns a shared handle for resource sharing
Use scenarios Single cURL request Share configuration and resources between multiple cURL requests
Share resources Shared resources are not supported Supports shared connections, cookies, DNS search and other resources
Operations when closed Close using curl_close() Close the shared handle using curl_share_close()

In general, curl_init() is used to initialize a single cURL request, while curl_share_init() is used to create shared resources for multiple cURL sessions. They are used and applied in different ways, but curl_share_init() can greatly improve performance when concurrent requests.