curl_share_init_persistent
(PHP 8 >= 8.5.0)
curl_share_init_persistent — Initialize a persistent cURL share handle.
说明
Initialize a persistent cURL share handle
with the given share options. Unlike curl_share_init(),
handles created by this function will not be destroyed at the end of the
PHP request. If a persistent share handle with the same set of
share_options
is found, it will be reused.
参数
share_options
-
A non-empty array of
CURL_LOCK_DATA_*
constants.注意:
CURL_LOCK_DATA_COOKIE
is not allowed and, if specified, this function will throw a ValueError. Sharing cookies between PHP requests may lead to inadvertently mixing up sensitive cookies between users.
返回值
Returns a CurlSharePersistentHandle.
错误/异常
-
If
share_options
is empty, this function throws a ValueError. -
If
share_options
contains a value not matching aCURL_LOCK_DATA_*
, this function throws a ValueError. -
If
share_options
containsCURL_LOCK_DATA_COOKIE
, this function throws a ValueError. -
If
share_options
contains a non-integer value, this function throws a TypeError.
示例
示例 #1 curl_share_init_persistent() example
This example will create a persistent cURL share handle and demonstrate
sharing connections between them. If this is executed in a long-lived
PHP SAPI, $sh
will survive between SAPI requests.
<?php
// Create or retrieve a persistent cURL share handle set to share DNS lookups and connections.
$sh = curl_share_init([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]);
// Initialize the first cURL handle and assign the share handle to it.
$ch1 = curl_init("http://example.com/");
curl_setopt($ch1, CURLOPT_SHARE, $sh);
// Execute the first cURL handle. This may reuse the connection from an earlier SAPI request.
curl_exec($ch1);
// Initialize the second cURL handle and assign the share handle to it.
$ch2 = curl_init("http://example.com/");
curl_setopt($ch2, CURLOPT_SHARE, $sh);
// Execute the second cURL handle. This will reuse the connection from $ch1.
curl_exec($ch2);
// Close the cURL handles
curl_close($ch1);
curl_close($ch2);
?>
参见
- curl_setopt() - 设置 cURL 传输选项
- curl_share_init() - 初始化 cURL 共享句柄
用户贡献的备注
备份地址:http://www.lvesu.com/blog/php/function.curl-share-init-persistent.php