Creates a persistent shared cURL handle for selected shared state across requests. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.
What It Does
Creates a persistent shared cURL handle for selected shared state across requests.
Syntax and Parameters
The examples below show a real call to curl_share_init_persistent with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.
Requirements: cURL extension; HTTP client and transfer-handling tools.
Compatibility and security: PHP 8.5 or later. This call returns CurlSharePersistentHandle; do not share per-user cookies.
Example 1: Core Behavior
PHP 8.5+; DNS cache can be shared persistently, but cookie sharing is explicitly forbidden.
<?php
if (PHP_VERSION_ID < 80500) { echo 'Requires PHP 8.5 or later'; return; }
$share = curl_share_init_persistent([CURL_LOCK_DATA_DNS]);
echo get_debug_type($share);
Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.
Example 2: Alternative or Failure Case
PHP 8.5+; DNS cache can be shared persistently, but cookie sharing is explicitly forbidden.
<?php
if (PHP_VERSION_ID < 80500) { echo 'Requires PHP 8.5 or later'; return; }
$share = curl_share_init_persistent([CURL_LOCK_DATA_SSL_SESSION]);
echo get_debug_type($share);
Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.
Practical Example: Applying the Rule
PHP 8.5+; DNS cache can be shared persistently, but cookie sharing is explicitly forbidden.
<?php
if (PHP_VERSION_ID < 80500) { echo 'Requires PHP 8.5 or later'; return; }
$share = curl_share_init_persistent([CURL_LOCK_DATA_CONNECT]);
echo get_debug_type($share);
In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.
Important Behavior and Edge Cases
PHP 8.5+; pass nonempty share option array, and CURL_LOCK_DATA_COOKIE is forbidden for privacy.
TLS validation, bounded timeouts and a destination allowlist are essential for network requests involving potentially untrusted targets.
Comparison with Related Functions or Patterns
| Approach | When it differs |
|---|---|
| curl_share_init_persistent | Creates a persistent shared cURL handle for selected shared state across requests. |
| Related API or pattern | curl_share_init makes a request-scoped CurlShareHandle instead. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Trying to persistently share cookies between user requests | PHP 8.5+; pass nonempty share option array, and CURL_LOCK_DATA_COOKIE is forbidden for privacy. |
| Substituting a related API without checking its semantics | curl_share_init makes a request-scoped CurlShareHandle instead. |
Key Takeaways
- Creates a persistent shared cURL handle for selected shared state across requests.
- PHP 8.5+; pass nonempty share option array, and CURL_LOCK_DATA_COOKIE is forbidden for privacy.
- curl_share_init makes a request-scoped CurlShareHandle instead.
Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.