PHPTS:一键免费搭建 Nginx + PHP + MySQL 运行环境

curl_share_setopt

(PHP 5 >= 5.5.0, PHP 7)

curl_share_setopt为 cURL 共享句柄设置选项。

说明

curl_share_setopt ( resource $sh , int $option , string $value ) : bool

为给定的 cURL 共享句柄设置一个选项。

参数

sh

一个由curl_share_init()函数返回的 cURL 共享句柄。

option

Option Description
CURLSHOPT_SHARE 指定要共享的数据类型。
CURLSHOPT_UNSHARE 指定不再共享的数据类型。

value

Value Description
CURL_LOCK_DATA_COOKIE 共享 cookie 数据。
CURL_LOCK_DATA_DNS 共享 DNS 缓存。注意,当你使用 cURL 多句柄时,默认所有添加在同一个多句柄的句柄都将会共享 DNS 缓存。
CURL_LOCK_DATA_SSL_SESSION 共享 SSL 的 session ID, 在重连同样的服务器时减少 SSL 握手时间。注意,SSL 的 session ID 在同一个的句柄中默认是重复使用的。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 curl_share_setopt() 函数的范例:

以下范例将会创建一个 cURL 共享句柄,并且往其中添加两个 cURL 句柄,最后共享这两个 cURL 句柄的 cookie 数据运行。

<?php
// Create cURL share handle and set it to share cookie data
$sh curl_share_init();
curl_share_setopt($shCURLSHOPT_SHARECURL_LOCK_DATA_COOKIE);

// Initialize the first cURL handle and assign the share handle to it
$ch1 curl_init("http://example.com/");
curl_setopt($ch1CURLOPT_SHARE$sh);

// Execute the first cURL handle
curl_exec($ch1);

// Initialize the second cURL handle and assign the share handle to it
$ch2 curl_init("http://php.net/");
curl_setopt($ch2CURLOPT_SHARE$sh);

// Execute the second cURL handle
//  all cookies from $ch1 handle are shared with $ch2 handle
curl_exec($ch2);

// Close the cURL share handle
curl_share_close($sh);

// Close the cURL handles
curl_close($ch1);
curl_close($ch2);
?>