PHP cURL curl_share_init() Function



The PHP cURL curl_share_init() function is used to initialize a cURL sharing handle. This handle allows data sharing between many cURL handles, for example, cookies or DNS cache.

Syntax

Below is the syntax of the PHP cURL curl_share_init() function −

CurlShareHandle curl_share_init ()

Parameters

This function does not take any parameters.

Return Value

The curl_share_init() function returns a cURL share handle on success and FALSE on failure.

PHP Version

First introduced in core PHP 5.5.0, the curl_share_init() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP cURL curl_share_init() function to initialize the cURL sharing handle and display message.

<?php
   // Create a cURL share handle
   $share_handle = curl_share_init();
   
   // Check for the initialization 
   if ($share_handle) {
       echo "The cURL share handle initialized successfully.";
   } else {
       echo "Failed to initialize cURL share handle.";
   }
   
   // Close the share handle when done
   curl_share_close($share_handle);
?>

Output

Here is the outcome of the following code −

The cURL share handle initialized successfully.

Example 2

In the below PHP code we will try to use the curl_share_init() function to share cookies between two cURL handles.

<?php
   // Create a cURL share handle
   $sh = curl_share_init();
   
   // Enable sharing cookies
   curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
   
   // Create first cURL handle
   $ch1 = curl_init('http://tutorialspoint.com');
   curl_setopt($ch1, CURLOPT_SHARE, $sh);
   curl_exec($ch1);
   
   // Create second cURL handle
   $ch2 = curl_init('http://tutorialspoint.com/search.htm');
   curl_setopt($ch2, CURLOPT_SHARE, $sh);
   curl_exec($ch2);
   
   // Close the cURL handles
   curl_close($ch1);
   curl_close($ch2);
   
   // Close the share handle
   curl_share_close($sh);
?> 

Output

This will generate the below output −

Found
The document has moved here.

Found
The document has moved here.

Example 3

Now the below code uses curl_share_init() function to share DNS cache among multiple cURL handles.

<?php
   // Create a cURL share handle
   $share_handle = curl_share_init();
   
   // Enable sharing DNS cache
   curl_share_setopt($share_handle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
   
   // Create first cURL handle
   $ch1 = curl_init('http://abc123.com');
   curl_setopt($ch1, CURLOPT_SHARE, $share_handle);
   curl_exec($ch1);
   
   // Create second cURL handle
   $ch2 = curl_init('http://abc123.com/another-page');
   curl_setopt($ch2, CURLOPT_SHARE, $share_handle);
   curl_exec($ch2);
   
   // Close the cURL handles
   curl_close($ch1);
   curl_close($ch2);
   
   // Close the share handle
   curl_share_close($share_handle);
?> 

Output

The above will display the HTMl content of the provided URLs −

curl_share_init Example 3

Example 4

In the following example, we are using the curl_share_init() function to handle multiple data types for sharing cookies and DNS cache.

<?php
   // create a cURL share handle
   $share_handle = curl_share_init();
   
   // enable sharing cookies and DNS cache
   curl_share_setopt($share_handle, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
   curl_share_setopt($share_handle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
   
   // create first cURL handle
   $ch1 = curl_init('http://abc123.com');
   curl_setopt($ch1, CURLOPT_SHARE, $share_handle);
   curl_exec($ch1);
   
   // create second cURL handle
   $ch2 = curl_init('http://abc123.com/another-page');
   curl_setopt($ch2, CURLOPT_SHARE, $share_handle);
   curl_exec($ch2);
   
   // Close the cURL handles
   curl_close($ch1);
   curl_close($ch2);
   
   // Close the share handle
   curl_share_close($share_handle);
?> 

Output

Following is the output of the above code −

curl_share_init Example 4
php_function_reference.htm
Advertisements